MySQL的索引 之组合索引使用
2013-12-13 15:21
399 查看
MySQL的索引 之组合索引使用
mysql中组合索引(联合索引)的使用
对于查询select * fromtable_name where uid=xx uidnd gpdm=xx 的形式
可以使用(uid,gpdm)这样子的组合索引,对于查询 select * fromtable_name where uid=xx 的形式
也可以使用(uid,gpdm)这样子的组合索引
但是对于查询select *from table_name where gpdm=xx 的形式
不可以使用(uid,gpdm) 这样子的组合索引,除了sql语句中有统计操作之外。
例如 select count(*) fromtable_name where gpdm=xxx and …gpdm=xxx;
![](http://blog.51cto.com/attachment/201312/151628381.png)
![](http://blog.51cto.com/attachment/201312/151652173.png)
possible_keys为NULL,但是key却为userid_2联合索引。using index 表示使用覆盖索引。
覆盖索引的定义:
官方解释
![](http://blog.51cto.com/attachment/201312/151734736.png)
即:一个包括所有要检索列的索引,不用再通过索引值去检索全部表数据,通过索引就可以直接取到数据。
Innodb引擎中的辅助索引(即第二索引) 包含了主键列信息。即
![](http://blog.51cto.com/attachment/201312/151807158.png)
例如:
![](http://blog.51cto.com/attachment/201312/151844292.png)
![](http://blog.51cto.com/attachment/201312/151904812.png)
userid_2为组合索引(userid,buy_duidte)
userid为索引userid
可以看到key键中直接选用了userid_2,并且type=ref. extruid中using index ,using where
如果强制使用userid索引
![](http://blog.51cto.com/attachment/201312/151932811.png)
![](http://blog.51cto.com/attachment/201312/151953609.png)
可以看到extruid中使用了using filesort, key为userid
mysql中组合索引(联合索引)的使用
对于查询select * fromtable_name where uid=xx uidnd gpdm=xx 的形式
可以使用(uid,gpdm)这样子的组合索引,对于查询 select * fromtable_name where uid=xx 的形式
也可以使用(uid,gpdm)这样子的组合索引
但是对于查询select *from table_name where gpdm=xx 的形式
不可以使用(uid,gpdm) 这样子的组合索引,除了sql语句中有统计操作之外。
例如 select count(*) fromtable_name where gpdm=xxx and …gpdm=xxx;
![](http://blog.51cto.com/attachment/201312/151628381.png)
![](http://blog.51cto.com/attachment/201312/151652173.png)
possible_keys为NULL,但是key却为userid_2联合索引。using index 表示使用覆盖索引。
覆盖索引的定义:
官方解释
![](http://blog.51cto.com/attachment/201312/151734736.png)
即:一个包括所有要检索列的索引,不用再通过索引值去检索全部表数据,通过索引就可以直接取到数据。
Innodb引擎中的辅助索引(即第二索引) 包含了主键列信息。即
![](http://blog.51cto.com/attachment/201312/151807158.png)
例如:
![](http://blog.51cto.com/attachment/201312/151844292.png)
![](http://blog.51cto.com/attachment/201312/151904812.png)
userid_2为组合索引(userid,buy_duidte)
userid为索引userid
可以看到key键中直接选用了userid_2,并且type=ref. extruid中using index ,using where
如果强制使用userid索引
![](http://blog.51cto.com/attachment/201312/151932811.png)
![](http://blog.51cto.com/attachment/201312/151953609.png)
可以看到extruid中使用了using filesort, key为userid
相关文章推荐
- Mysql索引的使用 - 组合索引 + 范围条件的处理
- 组合列mysql下的索引使用原则
- MySQL单列索引、组合索引的使用区别
- mysql 判断使用了组合索引的哪些列
- MySQL笔记之索引的使用
- mysql 索引的简单使用
- MySQL索引类型总结和使用技巧以及注意事项
- mysql索引的使用
- mysql 查询使用 in()条件时调用索引的争论
- Mysql建表与索引使用规范整理
- MySQL索引使用方法和性能优化
- mysql在数据库表中,使用索引可以大大提高查询速度
- mysql索引类型总结和使用技巧以及注意事项
- 错误使用MySQL前缀索引导致的慢查询
- mysql索引合并:一条sql可以使用多个索引
- 使用mysql索引技巧及注意事项
- MySQL使用CREATE INDEX创建索引
- Mysql数据分组GROUP BY 和HAVING,与WHERE组合使用
- MySQL查询优化技术之使用索引
- Mysql多个LEFT JOIN使用ORDER不能使用索引导致查询过慢的解决方案