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

Mysql优化之BTree索引使用规则

时间:2014-11-16 12:10:38      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   ar   使用   sp   文件   数据   

从一道题开始分析:

假设某个表有一个联合索引(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=? and c5=? order by c2,c3
E where c1=? and c2=? and c5=? order by c2,c3

有谁知道下面A-E能否可以使用索引!!为什么?

OK;开始

创建表:

insert into t 
values
('a1','a2','a3','a4','a5'),
('b1','b2','b3','b4','b5');

插入数据:

insert into t 
values
('a1','a2','a3','a4','a5'),
('b1','b2','b3','b4','b5');

添加索引:

alter table t add index c1234(c1,c2,c3,c4);

对第一种情况:说明c1,c2,c3,c4被使用

bubuko.com,布布扣

稍作改变:

bubuko.com,布布扣

bubuko.com,布布扣

使用group by 一般先生成临时文件,在进行排序

bubuko.com,布布扣

order by 哪?同上面类似啦

bubuko.com,布布扣

稍微改变一下,分析:知道原理都很容易啦!

bubuko.com,布布扣

上面问题答案是多少?反正我是不知道!

总结规律可得:

bubuko.com,布布扣


Mysql优化之BTree索引使用规则

标签:style   blog   http   color   ar   使用   sp   文件   数据   

原文地址:http://blog.csdn.net/buyingfei8888/article/details/41075435

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