Mysql 组合索引最左原则的疑惑
巴扎黑
巴扎黑 2017-04-17 16:16:54
0
1
635

如果test表有一个组合索引(a,b),执行如下两条语句。
explain select * from test where a=1 and b=1;
explain select * from test where b=1 and a=1;
执行结果显示这两条语句都用索引。根据最左原则,只有第一条使用索引,第二条不使用索引。不明白为什么,求解惑

巴扎黑
巴扎黑

Antworte allen(1)
黄舟

题主列出的情况,mysql会优化where子句的条件顺序,让查询符合索引顺序。

更具体点,上面的sql属于交集查询(都是and),在mysql中会使用Index Merge intersection algorithm算法来调整条件子句顺序。更详细的解释参见官方文档。

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!