文章詳情頁
Mysql 組合索引最左原則的疑惑
瀏覽:113日期:2022-06-15 17:53:39
問題描述
如果test表有一個組合索引(a,b),執行如下兩條語句。explain select * from test where a=1 and b=1;explain select * from test where b=1 and a=1;執行結果顯示這兩條語句都用索引。根據最左原則,只有第一條使用索引,第二條不使用索引。不明白為什么,求解惑
問題解答
回答1:題主列出的情況,mysql會優化where子句的條件順序,讓查詢符合索引順序。
更具體點,上面的sql屬于交集查詢(都是and),在mysql中會使用Index Merge intersection algorithm算法來調整條件子句順序。更詳細的解釋參見官方文檔。
相關文章:
1. python 利用subprocess庫調用mplayer時發生錯誤2. javascript - 有適合開發手機端Html5網頁小游戲的前端框架嗎?3. python - Pycharm的Debug用不了4. html - eclipse 標簽錯誤5. python - pandas按照列A和列B分組,將列C求平均數,怎樣才能生成一個列A,B,C的dataframe6. javascript - 關于apply()與call()的問題7. python - pycharm 自動刪除行尾空格8. python文檔怎么查看?9. datetime - Python如何獲取當前時間10. 安全性測試 - nodejs中如何防mySQL注入
排行榜
