提问:使用OR的查询为什么执行计划不使用索引?
时间:2011-06-15
来源:互联网
表结构:
SQL code
表数据:
SQL code
使用or的查询:
SQL code
疑问,为什么没有使用索引?
SQL code
mysql> show create table student\G *************************** 1. row *************************** Table: student Create Table: CREATE TABLE `student` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(30) NOT NULL, `sex` char(4) NOT NULL, `birth` date NOT NULL, `department` varchar(30) NOT NULL, PRIMARY KEY (`id`), KEY `index_birth_department` (`birth`,`department`), KEY `index_name` (`name`) ) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 1 row in set (0.00 sec)
表数据:
SQL code
mysql> select * from student; +----+------+-----+------------+------------+ | id | name | sex | birth | department | +----+------+-----+------------+------------+ | 1 | 张三 | 男 | 1985-02-06 | 信息学院 | | 2 | 赵六 | 女 | 1986-10-24 | 计算机学院 | | 3 | 李四 | 男 | 1991-02-15 | 英语系 | | 4 | 王五 | 女 | 1989-12-25 | 旅游系 | +----+------+-----+------------+------------+ 4 rows in set (0.00 sec)
使用or的查询:
SQL code
mysql> explain select * from student where name='张三' or id=2\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: student type: ALL possible_keys: PRIMARY,index_name key: NULL key_len: NULL ref: NULL rows: 4 Extra: Using where 1 row in set (0.00 sec)
疑问,为什么没有使用索引?
作者: zeldady 发布时间: 2011-06-15
你一共才四条记录, MYSQL判断根本不需要走两次索引再拼接结果集。
作者: ACMAIN_CHM 发布时间: 2011-06-15
没法用索引定位数据
作者: rucypli 发布时间: 2011-06-15
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28