码迷,mamicode.com
首页 > 数据库 > 详细

数据库优化 - 多列索引经典题目

时间:2017-09-01 10:51:54      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:创建   技术   外排序   nbsp   ges   log   引用   image   数据库优化   

题目

假设某个表有一个联合索引(c1,c2,c3,c4)一下——只能使用该联合索引的c1,c2,c3部分

A where c1=x and c2=x and c4>x and c3=x

B where c1=x and c2=x and c4=x order by c3

C where c1=x and c4= x group by c3,c2

D where c1=x and c5=x order by c2,c3

E where c1=x and c2=x and c5=? order by c2,c3

创建表,并创建相应索引,插入数

技术分享

 

对于A:

c1=x and c2=x and c4>x and c3=x  <==等价==> c1=x and c2=x and c3=x and c4>x

因此 c1,c2,c3,c4都能用上. 如下:

 技术分享

对于B:

select * from t4 where c1=1 and c2=2 and c4=3 order by c3

c1 ,c2索引用上了,在c2用到索引的基础上,c3是排好序的,因此不用额外排序.

而c4没发挥作用.

 技术分享

如果用c5排序,因为c5不是索引,所以,要额外用到filesort进行排序

对于 C:

只用到c1索引,因为group by c3,c2的顺序无法利用c2,c3索引

 技术分享

D语句:

C1确定的基础上,c2是有序的,C2之下C3是有序的,因此c2,c3发挥的排序的作用.

因此,没用到filesort

 技术分享

E:

这一句等价与 elect * from t4 where c1=1 and c2=3 and c5=2 order by c3;

因为c2的值既是固定的,参与排序时并不考虑

 技术分享

 

数据库优化 - 多列索引经典题目

标签:创建   技术   外排序   nbsp   ges   log   引用   image   数据库优化   

原文地址:http://www.cnblogs.com/meidang/p/7461985.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!