MySql Index在范围扫描中的工作方式不同

时间:2017-03-06 06:01:35

标签: mysql indexing

select * from [Table] where [DateColumn] >= [dateValue1] and [DateColumn] <= [dateValue2];

索引没有发生

select * from [Table] where [DateColumn] > [dateValue1] and [DateColumn] < [dateValue2];

索引正在发生。

日期列已编入索引

任何人都可以澄清为什么会出现这种差异。

查询

select * from test_index where test_date >= '2016-11-15' and test_date <= '2016-12-02';

解释

   +----+-------------+------------+------------+------+-------------------------------------------------+------+---------+------+-------+----------+-------------+
| id | select_type | table      | partitions | type | possible_keys                                   | key  | key_len | ref  | rows  | filtered | Extra       |
+----+-------------+------------+------------+------+-------------------------------------------------+------+---------+------+-------+----------+----]---------+
|  1 | SIMPLE      | test_index | NULL       | ALL  | idx_negativebalance_firstnegativeoccurrencedate | NULL | NULL    | NULL | 16899 |    17.92 | Using where |
+----+-------------+------------+------------+------+-------------------------------------------------+------+---------+------+-------+----------+-------------+

查询2

从test_index中选择*,其中test_date&gt; '2016-11-15'和test_date&lt; '2016年12月2日';

+----+-------------+------------+------------+-------+-------------------------------------------------+-------------------------------------------------+---------+------+------+----------+-----------------------+
| id | select_type | table      | partitions | type  | possible_keys                                   | key   | key_len | ref  | rows | filtered | Extra                 |
+----+-------------+------------+------------+-------+-------------------------------------------------+-------------------------------------------------+---------+------+------+----------+-----------------------+
|  1 | SIMPLE      | test_index | NULL       | range | idx_negativebalance_firstnegativeoccurrencedate | idx_negativebalance_firstnegativeoccurrencedate | 8       | NULL | 2727 |   100.00 | Using index condition |
+----+-------------+------------+------------+-------+-------------------------------------------------+-------------------------------------------------+---------+------+------+----------+-----------------------+

1 个答案:

答案 0 :(得分:2)

在我的快速测试中,如果85%的结果属于该范围,我可以看到该指数被忽略。你可能只是处于85%数字的边缘吗?

[密集是一个数字超过一百万的列,范围从1-100,并且在整个董事会中的分布大致相等]

mysql> EXPLAIN SELECT * FROM my_table WHERE dense >= 84 AND dense <= 100;
+----+-------------+----------+------+---------------+------+---------+------+---------+-------------+
| id | select_type | table    | type | possible_keys | key  | key_len | ref  | rows    | Extra       |
+----+-------------+----------+------+---------------+------+---------+------+---------+-------------+
|  1 | SIMPLE      | my_table | ALL  | dense         | NULL | NULL    | NULL | 1048576 | Using where |
+----+-------------+----------+------+---------------+------+---------+------+---------+-------------+
1 row in set (0.00 sec)

mysql> EXPLAIN SELECT * FROM my_table WHERE dense >= 85 AND dense <= 100;
+----+-------------+----------+-------+---------------+-------+---------+------+--------+-------------+
| id | select_type | table    | type  | possible_keys | key   | key_len | ref  | rows   | Extra       |
+----+-------------+----------+-------+---------------+-------+---------+------+--------+-------------+
|  1 | SIMPLE      | my_table | range | dense         | dense | 4       | NULL | 168389 | Using where |
+----+-------------+----------+-------+---------------+-------+---------+------+--------+-------------+
1 row in set (0.00 sec)