组合索引的最左优先原则

组合索引的最左优先原则:
组合索引的第一个字段必须出现在查询组句中,这个索引才会被用到。果有一个组合索引(col_a,col_b,col_c),下面的情况都会用到这个索引:

col_a = "some value";col_a = "some value" and col_b = "some value";col_a = "some value" and col_b = "some value" and col_c = "some value";col_b = "some value" and col_a = "some value" and col_c = "some value";

对于最后一条语句,mysql会自动优化成第三条的样子~~。下面的情况就不会用到索引:

col_b = "aaaaaa";col_b = "aaaa" and col_c = "cccccc";

我是标题

若你觉得我的文章对你有帮助,欢迎点击上方按钮对我打赏

扫描二维码,分享此文章