explain select id, customer_name, company_name from t_user_info where customer_name > '查理一世'
# type: all, possible_keys: null, key: null
# idx_customer_name 索引生效
explain select id, customer_name, company_name from t_user_info where customer_name > company_name
复制代码
联合索引不满意最左匹配原则
联合索引遵从最左匹配原则,所谓最左匹配原则,就是假如 SQL 语句用到了联合索引中的最左边的索引,那么这条 SQL 语句就可以利用这个联合索引去进行匹配。值得注意的是,当遇到范围查询(>、 1 and b = 2 and c = 3[/code]我们知道索引是用 B+Tree 实现的,假如只对 a 字段建立普通索引,那么 B+Tree 根据 a 字段排序。假如对 a、b、c 建立联合索引,那么首先根据 a 字段排序,假如 a 字段值雷同,再根据 b 字段排序,假如 b 字段值也雷同,再根据 c 字段排序。因此,利用联合索引必须按照从左到右,也就是字段排序的序次,只有先用了 a,才能接着利用 b,利用了 b 才能接着利用 c