【MySQL】使用不到索引的情况
2015-01-27 15:50
519 查看
MySQL使用不到索引的情况有很多,今天具体操作了一番,总结了些常见的情况
创建以下表,有四个字段,其中name和password做了关联索引,id为主键索引
1. 使用like关键字。like关键字使用有两种情况
a) 有通配符且在关键字的最左边,会使用不到索引,如下示例
b) 无通配符或者通配符不在关键字最左边,能够使用到索引,如'a%bc','abc%'
2. 联合索引中,只有声明在最左边的字段被索引上了,在同时使用两个列或使用最左边的列作为查询条件的时候会使用到索引
使用password字段作为查询条件未使用到索引
3. 使用补集的情况 (!=,not in)
4. 使用了错误的数据类型
范例如下,name字段使用了varchar类型存储,虽然不加引号当做int类型时候可以解析,但是不会用到索引
5. 在未添加索引字段使用or关键字
创建以下表,有四个字段,其中name和password做了关联索引,id为主键索引
mysql> show create table users \G *************************** 1. row *************************** Table: users Create Table: CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `password` varchar(255) DEFAULT NULL, `email` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`), KEY `loginIndex` (`name`,`password`) ) ENGINE=InnoDB AUTO_INCREMENT=6428544 DEFAULT CHARSET=utf8 1 row in set (0.00 sec)
1. 使用like关键字。like关键字使用有两种情况
a) 有通配符且在关键字的最左边,会使用不到索引,如下示例
b) 无通配符或者通配符不在关键字最左边,能够使用到索引,如'a%bc','abc%'
mysql> explain select * from users where name like '%abc' \G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: users type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 6257258 Extra: Using where 1 row in set (0.00 sec)
2. 联合索引中,只有声明在最左边的字段被索引上了,在同时使用两个列或使用最左边的列作为查询条件的时候会使用到索引
使用password字段作为查询条件未使用到索引
mysql> explain select * from users where password = '123' \G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: users type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 6257258 Extra: Using where 1 row in set (0.00 sec)
3. 使用补集的情况 (!=,not in)
mysql> explain select * from users where name != 'aaa' \G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: users type: ALL possible_keys: loginIndex key: NULL key_len: NULL ref: NULL rows: 6257258 Extra: Using where 1 row in set (0.04 sec)
mysql> explain select * from users where name not in ('aaa', 'bbb', 'ccc') \G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: users type: ALL possible_keys: loginIndex key: NULL key_len: NULL ref: NULL rows: 6257258 Extra: Using where 1 row in set (0.04 sec)
4. 使用了错误的数据类型
范例如下,name字段使用了varchar类型存储,虽然不加引号当做int类型时候可以解析,但是不会用到索引
mysql> explain select * from users where name = 123456 \G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: users type: ALL possible_keys: loginIndex key: NULL key_len: NULL ref: NULL rows: 6257258 Extra: Using where 1 row in set (0.00 sec)
5. 在未添加索引字段使用or关键字
mysql> explain select * from users where name = 'a' or password = 'b' \G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: users type: ALL possible_keys: loginIndex key: NULL key_len: NULL ref: NULL rows: 6257258 Extra: Using where 1 row in set (0.00 sec)
相关文章推荐
- Mysql查看索引使用情况
- MySQL 存在索引但是不被使用的情况
- mysql 查看索引使用情况
- mysql 查看索引使用情况
- MySQL 5.5 分区性能测试之索引使用情况
- MYSQL 索引类型、什么情况下用不上索引、什么情况下不推荐使用索引
- mysql 理解索引,添加索引,使用索引(哪些情况会导致索引失效)
- mysql什么情况下不会使用索引
- MySQL InnoDB 默认行级锁,行级锁都是基于索引,SQL 语句用不到索引是不会使用行级锁的,而会使用表级锁把整张表锁住
- MySQL Order By 使用索引的情况
- mysql 索引B-Tree类型对索引使用的生效和失效情况详解
- mysql 中不会使用索引的几种情况
- mysql如何查看索引使用情况以及优化 - guols0612
- MySQL 5.5 分区性能测试之索引使用情况
- 测试mysql查询中参数整形和字符串类型错误索引使用情况
- MySql是怎么使用的索引,在哪些情况下会使用到索引
- mysql使用(不适用)索引的情况
- MySQL中查询不使用索引的情况汇总
- mysql 索引长度解释及不使用索引的一种特殊情况
- 总结mysql不使用索引情况以及如何优化sql语句