联合索引,在哪些查询条件下可能会用到

在数据库建了联合索引,abc三字段。
第一:单独使用a,或b,或c查,会不会用到索引
第二:使用ab,bc,ac查,会不会用到索引
使用explain

EXPLAIN select * from mp_dialogue_record p where p.agent_id = 199969;
EXPLAIN select * from mp_dialogue_record p where p.im_type = 2;
EXPLAIN select * from mp_dialogue_record p where p.insert_date = '2018-05-24';
EXPLAIN select * from mp_dialogue_record p where p.agent_id = 199969 and p.im_type = 2;
EXPLAIN select * from mp_dialogue_record p where p.im_type = 2 and p.agent_id = 199969;
EXPLAIN select * from mp_dialogue_record p where p.im_type = 2 and  p.insert_date = '2018-05-24';
EXPLAIN select * from mp_dialogue_record p where p.agent_id = 2 and  p.insert_date = '2018-05-24';
EXPLAIN select * from mp_dialogue_record p where p.agent_id = 199969 and p.im_type = 2 and  p.insert_date = '2018-05-24';

结果就是,abc的联合索引,只要查询条件中,用到a了,就用到索引。
所以创建联合索引,还需要注意顺序。

猜你喜欢

转载自blog.csdn.net/qazwsx081/article/details/81627249