explain sql語句乙個語句,得到如下結果,為什麼已經建立了t_bill_invests.bid_id的索引,但卻沒有顯示using index,而是顯示all掃瞄方式呢,原來這還與select裡面的內容有關
17:08:20 select t_bill_invests.`receive_corpus`, t_bill_invests.`receive_interest`, t_bids.`period`, t_bill_invests.`receive_time`, `t_bids`.`title` as `title`, `t_bids`.`period_unit` as `period_unit` from `t_bill_invests` left join `t_bids` on `t_bids`.`id` = `t_bill_invests`.`bid_id` left join t_invests on t_bill_invests.`invest_id` = `t_invests`.`id` where `t_bill_invests`.`status` = -1 and `t_invests`.`transfer_status` <> - (1) and t_invests.user_id = 3 order by t_bill_invests.receive_time asc limit 5 5 row(s) returned
23.728 sec / 0.000 sec
我們先來做幾個小實驗來說明此問題,如下,我們在user_id與bid_id上都是有索引的,但是explain出來的結果卻不一樣:
這是因為user_id並沒有在條件語句中,mysql會通過bid_id找到相關的值,再進一步回表掃瞄,故會顯示為all,但此時的掃瞄已經不是真的全表掃瞄,而是根據條件中篩選出來的值再來找到對應的user_id
進一步的,如果我們新增乙個覆蓋索引:
可以發現,現在雖然是select user_id但也可以走索引了。
乙個問題是我們並不能把所有的select裡面的列都新增為覆蓋索引,所以我們只要保證條件裡面所涉及的列有相應的索引,這個速度都會是非常快的。
通過檢查這條語句,發現t_bill_invests.`invest_id`這一列還沒有新增索引,故我們新增上:
alter table t_bill_invests add index index_bill_inv_invest_id(invest_id)
再次執行:
17:12:38 select t_bill_invests.`receive_corpus`, t_bill_invests.`receive_interest`, t_bids.`period`, t_bill_invests.`receive_time`, `t_bids`.`title` as `title`, `t_bids`.`period_unit` as `period_unit` from `t_bill_invests` left join `t_bids` on `t_bids`.`id` = `t_bill_invests`.`bid_id` left join t_invests on t_bill_invests.`invest_id` = `t_invests`.`id` where `t_bill_invests`.`status` = -1 and `t_invests`.`transfer_status` <> - (1) and t_invests.user_id = 3 order by t_bill_invests.receive_time asc limit 5 5 row(s) returned
1.763 sec / 0.000 sec
可以發現,時間已經大大減少了,目標完成。
抽象乙個分頁的方法出來
controller public class messagecontroller extends basebbsmessagecontroller 遊客通道主頁 分頁的實現 public mappaging string oripageno,int totalrecords else 計算共顯示多...
關於乙個判斷Integer數是否為回文數的演算法問題
如題 determine whether an integer is a palindrome.do this without extra space.自己雖然也寫出了能得到正確結果的答案,但看到大神寫的後,就無顏貼自己繁瑣的東西了。看到的演算法如下 public boolean ispalindr...
分享乙個vue裡面關於防止模板跑出來的小技巧
lang en charset utf 8 titletitle head type text v model city type button value 查詢 click findweather city href click changecity 北京 北京a href click chang...