mysql语句中索引可能失效的情况

一、创建两张表 表a 表b  选择不同的存储引擎以InnoDB和MyISAM为例

表a

CREATE TABLE `a` (
`ID`  int NOT NULL AUTO_INCREMENT ,
`name`  varchar(32) NOT NULL ,
`score`  int UNSIGNED NOT NULL ,
PRIMARY KEY (`ID`),
UNIQUE INDEX `name` (`name`) 
)
ENGINE=InnoDB
;
表b

CREATE TABLE `b` (
`ID`  int NOT NULL AUTO_INCREMENT ,
`name`  varchar(32) NOT NULL ,
`score`  int UNSIGNED NOT NULL ,
PRIMARY KEY (`ID`),
UNIQUE INDEX `name` (`name`) 
)
ENGINE=MyISAM
;
新增数据

mysql> select * from a;
+----+--------------+-------+
| ID | name         | score |
+----+--------------+-------+
|  1 | 德玛西亚     |    88 |
|  2 | 艾欧尼亚     |    78 |
|  3 | 无畏先锋     |    66 |
|  4 | 暗影岛       |    99 |
|  5 | 黑色玫瑰     |    43 |
|  6 | 诺克萨斯     |    56 |
|  7 | 班德尔城     |    72 |
|  8 | 雷瑟守备     |    30 |
+----+--------------+-------+
8 rows in set (0.00 sec)

mysql> select * from b;
+----+--------------+-------+
| ID | name         | score |
+----+--------------+-------+
|  1 | 德玛西亚     |    88 |
|  2 | 艾欧尼亚     |    78 |
|  3 | 无畏先锋     |    66 |
|  4 | 暗影岛       |    99 |
|  5 | 黑色玫瑰     |    43 |
|  6 | 诺克萨斯     |    56 |
|  7 | 班德尔城     |    72 |
|  8 | 雷瑟守备     |    30 |
+----+--------------+-------+
8 rows in set (0.00 sec)


二、开始测试

1.where后面使用了or 索引可能会失效

innodb和myisam:字段都建立索引时候有效,否则索引失效

mysql> explain select name from a where id = 1 or score =66;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | a     | NULL       | ALL  | PRIMARY       | NULL | NULL    | NULL |    8 |    23.44 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

mysql> explain select score from a where id = 1 or name ='暗影岛';
+----+-------------+-------+------------+-------------+---------------+--------------+---------+------+------+----------+-----------------------------
-----------+
| id | select_type | table | partitions | type        | possible_keys | key          | key_len | ref  | rows | filtered | Extra
           |
+----+-------------+-------+------------+-------------+---------------+--------------+---------+------+------+----------+-----------------------------
-----------+
|  1 | SIMPLE      | a     | NULL       | index_merge | PRIMARY,name  | PRIMARY,name | 4,98    | NULL |    2 |   100.00 | Using union(PRIMARY,name); U
sing where |
+----+-------------+-------+------------+-------------+---------------+--------------+---------+------+------+----------+-----------------------------
-----------+
1 row in set, 1 warning (0.00 sec)

2.where后面使用了<>  != 索引可能会失效
innodb:id字段索引可用

myisam:id字索引失效

mysql> explain select * from a where id<>2;
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type  | possible_keys | key     | key_len | ref  | rows | filtered | Extra       |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | a     | NULL       | range | PRIMARY       | PRIMARY | 4       | NULL |    7 |   100.00 | Using where |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

mysql> explain select * from b where id<>2;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | b     | NULL       | ALL  | PRIMARY       | NULL | NULL    | NULL |    8 |   100.00 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

3.使用了like '%....'索引会失效,查询时候最左原则
innodb和myisam:name字段索引失效

mysql> explain select * from a where name like '%斯%';
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | a     | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    8 |    12.50 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

mysql> explain select * from a where name like '斯%';
+----+-------------+-------+------------+-------+---------------+------+---------+------+------+----------+-----------------------+
| id | select_type | table | partitions | type  | possible_keys | key  | key_len | ref  | rows | filtered | Extra                 |
+----+-------------+-------+------------+-------+---------------+------+---------+------+------+----------+-----------------------+
|  1 | SIMPLE      | a     | NULL       | range | name          | name | 98      | NULL |    1 |   100.00 | Using index condition |
+----+-------------+-------+------------+-------+---------------+------+---------+------+------+----------+-----------------------+
1 row in set, 1 warning (0.00 sec)

mysql> explain select * from b where name like '斯%';
+----+-------------+-------+------------+-------+---------------+------+---------+------+------+----------+-----------------------+
| id | select_type | table | partitions | type  | possible_keys | key  | key_len | ref  | rows | filtered | Extra                 |
+----+-------------+-------+------------+-------+---------------+------+---------+------+------+----------+-----------------------+
|  1 | SIMPLE      | b     | NULL       | range | name          | name | 98      | NULL |    1 |   100.00 | Using index condition |
+----+-------------+-------+------------+-------+---------------+------+---------+------+------+----------+-----------------------+
1 row in set, 1 warning (0.00 sec)

mysql> explain select * from b where name like '%斯%';
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | b     | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    8 |    12.50 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)


暂时写到这里- -写的不全,第一次写博客,写的比较简单,欢迎指正!


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值