MySQL 查询优化之 or
2014-07-03 21:57
197 查看
当使用or的时候是不会用到索引的
解决办法就是用union替换or
高下立判
mysql> explain SELECT * FROM aladdin_resource WHERE state = 1 OR state = 2; +----+-------------+------------------+------+---------------+------+---------+------+-------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------------+------+---------------+------+---------+------+-------+-------------+ | 1 | SIMPLE | aladdin_resource | ALL | state | NULL | NULL | NULL | 59074 | Using where | +----+-------------+------------------+------+---------------+------+---------+------+-------+-------------+ 1 row in set (0.00 sec)
解决办法就是用union替换or
explain select * from aladdin_resource where state=1 union select * from aladdin_resource where state=2; +----+--------------+------------------+------+---------------+-------+---------+-------+-------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+--------------+------------------+------+---------------+-------+---------+-------+-------+-------------+ | 1 | PRIMARY | aladdin_resource | ref | state | state | 2 | const | 383 | Using where | | 2 | UNION | aladdin_resource | ref | state | state | 2 | const | 21370 | Using where | | NULL | UNION RESULT | <union1,2> | ALL | NULL | NULL | NULL | NULL | NULL | | +----+--------------+------------------+------+---------------+-------+---------+-------+-------+-------------+ 3 rows in set (0.05 sec)
高下立判
相关文章推荐
- 关于mysql 使用or连接索引失效引起的慢查询优化的初步实践
- MySQL查询优化系列讲座之数据类型与效率
- MySQL查询优化系列讲座之调度和锁定
- MySQL查询优化系列讲座之查询优化器
- MySQL查询优化讲座之管理员的优化措施
- MySQL查询优化技术系列讲座之使用索引
- MySQL查询优化技术讲座
- MySQL查询的性能优化
- MySQL查询优化讲座之管理员的优化措施
- MySQL查询优化系列讲座之查询优化器
- MySQL查询优化程序
- MySQL查询优化程序
- MySQL查询优化技术讲座
- 优化 MySQL 查询的 Limit 参数
- 优化 MySQL 查询的 Limit 参数
- MySQL查询优化系列讲座之调度和锁定 选择自 arcow 的 Blog
- mysql 常用命令和查询优化
- MySQL查询优化--数据类型与效率
- MySQL查询优化系列讲座之调度和锁定
- MySQL 查询中的分页思路的优化