1: 建立索引就是为了查询快些, 对于复合索引,在查询使用时,最好将条件顺序按找索引的顺序,这样效率最高。 如: IDX1:create index idx1 on table1(col2,col3,col5) select * from table1 where col2=A and col3=B and col5=D 2: 在place,date,amount上的组合索引 select count() from record where date > '19991201' and date < '19991214' and amount > 2000 (26秒) select date,sum(amount) from record group by date (27秒) select count() from record where date > '19990901' and place in ('BJ, 'SH')(< 1秒) ---- 分析: ---- 这是一个不很合理的组合索引,因为它的前导列是place,第一和第二条SQL没有引 用place,因此也没有利用上索引;第三个SQL使用了place,且引用的所有列都包含在组 合索引中,形成了索引覆盖,所以它的速度是非常快的。 ---- 4.在date,place,amount上的组合索引 select count() from record where date > '19991201' and date < '19991214' and amount > 2000(< 1秒) select date,sum(amount) from record group by date (11秒) select count() from record where date > '19990901' and place in ('BJ','SH')(< 1秒) ---- 分析: ---- 这是一个合理的组合索引。它将date作为前导列,使每个SQL都可以利用索引,并 且在第一和第三个SQL中形成了索引覆盖,因而性能达到了最优。
转载于:https://my.oschina.net/ZhangJava/blog/750720