数据库优化 - 多列索引经典题目
題目
假設某個表有一個聯合索引(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的值既是固定的,參與排序時并不考慮
?
?
轉載于:https://www.cnblogs.com/meidang/p/7461985.html
總結
以上是生活随笔為你收集整理的数据库优化 - 多列索引经典题目的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: net 中web.config一个配置文
- 下一篇: MSSQL 如何采用sql语句 获取建表