57,064
社区成员




select * from t where c1 = 100 and c2 = 100
select * from t where c2 = 100 and c1 = 100
[/quote]
第二句不会用到复合索引吧?create table t (c1 int , c2 int , c3 int , c4 int)
场景1
create index t_ix1 on t(c1) ;
create index t_ix2 on t(c2) ;
此时,你执行
select * from t where c1 = 100 -- 会用到 t_ix1
select * from t where c2 = 100 -- 会用到 t_ix2
select * from t where c1 = 100 and c2 = 100 -- 只能用到 t_ix1 和 t_ix2 中的一个。
场景2
create index t_ix1 on t(c1 ,c2)
create index t_ix2 on t(c2)
你再执行
select * from t where c1 = 100 -- 会用到 t_ix1
select * from t where c2 = 100 -- 会用到 t_ix2 ,如果没有 t_ix2 ,就会全表扫描
select * from t where c1 = 100 and c2 = 100 -- 会用到 t_ix1
另,条件中的各个语句 在顺序上,不会影响执行计划
select * from t where c1 = 100 and c2 = 100
select * from t where c2 = 100 and c1 = 100