你好,我有这个问题
SELECT * FROM customers WHERE height > 180 AND height < 200
这是对其应用 explain
的结果:
mysql> explain SELECT * FROM customers WHERE height > 180 AND height < 200;
+----+-------------+-----------+------+---------------+------+---------+------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+---------------+------+---------+------+-------+-------------+
| 1 | SIMPLE | customers | ALL | height | NULL | NULL | NULL | 10423 | Using where |
+----+-------------+-----------+------+---------------+------+---------+------+-------+-------------+
请注意,height
字段有一个索引,但不知何故……
height
字段是一个整数。我不知道。我期待它被使用并且 type
是 range
有人吗?
最佳答案
问题出在指定的范围上。显然太松了。在这种特殊情况下,Mysql 更喜欢全扫描而不是使用索引。如果我限制范围,或增加记录数,则使用 height
索引,并且搜索访问策略按照手册变成 range
。
关于mysql - 范围查询中未使用索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16012715/