奇怪的慢sql

我們先來看2條sql

第一條:

select * from acct_trans_log WHERE  acct_id = 1000000000009000757 order by create_time desc limit 0,10

  

第二條:

 select * from acct_trans_log WHERE  acct_id = 1000000000009003061 order by create_time desc limit 0,10

表的索引及數(shù)據(jù)總情況:

 

索引:acct_id,create_time分別是單列索引,數(shù)據(jù)庫總數(shù)據(jù)為500w

通過acct_id過濾出來的結(jié)果集在1w條左右

 

查詢結(jié)果:第一條要5.018s,第二條0.016s

為什么會是這樣的結(jié)果呢?第一,acct_id和create_time都有索引,不應(yīng)該出現(xiàn)5s查詢時間這么慢啊

 

那么先來看執(zhí)行計劃

第一條sql執(zhí)行計劃:

平面設(shè)計培訓,網(wǎng)頁設(shè)計培訓,美工培訓,游戲開發(fā),動畫培訓

 第二條執(zhí)行計劃:

平面設(shè)計培訓,網(wǎng)頁設(shè)計培訓,美工培訓,游戲開發(fā),動畫培訓

 仔細觀察會發(fā)現(xiàn),索引只使用了idx_create_time,沒有用到idx_acct_id

這能解釋第一條sql很慢,因為where查詢未用到索引,那么第二條為什么這么快?

看起來匪夷所思,其實搞

網(wǎng)友評論