生產(chǎn)環(huán)境MySQL索引時效的排查過程
早上收到開發(fā)同學(xué)求助,有個SQL查詢耗時特別長,看了執(zhí)行計劃發(fā)現(xiàn)沒有走索引,但是不知道原因在哪里,我們一起來分析一下。
mysql>explain SELECT * FROM artisan_income WHERE parent_id IN ( 222645481, 222583953, 222181775, 222180931, 222081126, 221678753, 221616102, 221591783, 221219312, 221195482, 221118672, 220763129, 220654289, 220633930, 220323633, 220227641, 219825564, 219720338, 219321345, 219291958 ) \G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: artisan_income partitions: type: ALL possible_keys: idx_parent_id key: key_len: ref: rows: 20711352 filtered: 100 Extra: Using where
確實是全表掃描,帶著疑問我們把生產(chǎn)環(huán)境數(shù)據(jù)同步到測試庫,方便測試,然后在測試環(huán)境進(jìn)行查詢。
mysql>explain SELECT * FROM artisan_income WHERE parent_id IN ( 222645481, 222583953, 222181775, 222180931, 222081126, 221678753, 221616102, 221591783, 221219312, 221195482, 221118672, 220763129, 220654289, 220633930, 220323633, 220227641, 219825564, 219720338, 219321345, 219291958 ) \G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: artisan_income partitions: type: range possible_keys: idx_parent_id key: idx_parent_id key_len: 5 ref: rows: 1870780 filtered: 100 Extra: Using index condition
發(fā)現(xiàn)在測試環(huán)境就用到了parent_id字段的索引,生產(chǎn)庫和測試庫同樣都是5.7的版本,數(shù)據(jù)也幾乎一樣,但是執(zhí)行計劃不一樣,第一時間想到了統(tǒng)計信息的問題,于是進(jìn)行analyze table
analyze table artisan_income;
然后查看執(zhí)行計劃:
mysql>explain SELECT * FROM artisan_income WHERE parent_id IN ( 222645481, 222583953, 222181775, 222180931, 222081126, 221678753, 221616102, 221591783, 221219312, 221195482, 221118672, 220763129, 220654289, 220633930, 220323633, 220227641, 219825564, 219720338, 219321345, 219291958 ) \G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: artisan_income partitions: type: range possible_keys: idx_parent_id key: idx_parent_id key_len: 5 ref: rows: 1901880 filtered: 100 Extra: Using index condition
發(fā)現(xiàn)執(zhí)行計劃已經(jīng)恢復(fù)正常。
mysql>SELECT * FROM artisan_income WHERE parent_id IN ( 222645481, 222583953, 222181775, 222180931, 222081126, 221678753, 221616102, 221591783, 221219312, 221195482, 221118672, 220763129, 220654289, 220633930, 220323633, 220227641, 219825564, 219720338, 219321345, 219291958 ) \G 返回行數(shù):[0],耗時:2 ms.
到此這篇關(guān)于生產(chǎn)環(huán)境MySQL索引時效的排查過程的文章就介紹到這了,更多相關(guān)MySQL索引排查內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySQL數(shù)據(jù)庫事務(wù)隔離級別介紹(Transaction Isolation Level)
這篇文章主要介紹了MySQL數(shù)據(jù)庫事務(wù)隔離級別(Transaction Isolation Level) ,需要的朋友可以參考下2014-05-05