我有一张表格如下:
desc log;
+-------------------+-------------------------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+-------------------------------+------+-----+-------------------+-------+
| time | timestamp | NO | MUL | CURRENT_TIMESTAMP | |
| severity | enum('0','1','2','3','4','5') | NO | | 1 | |
| user_id | bigint(20) | YES | MUL | NULL | |
| user_ip | varchar(15) | YES | MUL | NULL | |
| module | varchar(30) | NO | | NULL | |
| message | text | NO | | NULL | |
| blog_id | bigint(20) | NO | MUL | NULL | |
| event_type | varchar(255) | YES | MUL | NULL | |
| post_id | bigint(20) | YES | | NULL | |
| data | longtext | YES | | NULL | |
| data_integer | tinyint(4) | YES | | NULL | |
| data_integer_type | varchar(100) | YES | | NULL | |
+-------------------+-------------------------------+------+-----+-------------------+-------+
该表包含以下索引:
show indexes from log;
+-------------+------------+----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------------+------------+----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| log | 1 | log_event_type_idx | 1 | event_type | A | 30 | NULL | NULL | YES | BTREE | |
| log | 1 | log_user_id_idx | 1 | user_id | A | 10938 | NULL | NULL | YES | BTREE | |
| log | 1 | log_blog_id_idx | 1 | blog_id | A | 24064 | NULL | NULL | | BTREE | |
| log | 1 | log_user_ip_idx | 1 | user_ip | A | 24064 | NULL | NULL | YES | BTREE | |
| log | 1 | log_time_idx | 1 | time | A | 240647 | NULL | NULL | | BTREE | |
+-------------+------------+----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
当我查询8月或9月的数据时,它使用索引并且工作正常。但对于Oct数据,它不使用索引。表中最大日期为“2012-10-15”。 我添加了解释计划。
explain
select *
from log
where time >= '2012-08-01 00:00:00' AND time < '2012-09-01 00:00:00'
+----+-------------+-------------+-------+----------------------+----------------------+---------+------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------+-------+----------------------+----------------------+---------+------+-------+-------------+
| 1 | SIMPLE | log | range | log_time_idx | log_time_idx | 4 | NULL | 57306 | Using where |
+----+-------------+-------------+-------+----------------------+----------------------+---------+------+-------+-------------+
1 row in set (0.04 sec)
explain
select *
from log
where time >= '2012-09-01 00:00:00' AND time < '2012-10-01 00:00:00'
+----+-------------+-------------+-------+----------------------+----------------------+---------+------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------+-------+----------------------+----------------------+---------+------+-------+-------------+
| 1 | SIMPLE | log | range | log_time_idx | log_time_idx | 4 | NULL | 42569 | Using where |
+----+-------------+-------------+-------+----------------------+----------------------+---------+------+-------+-------------+
explain
select *
from log
where time >= '2012-10-01 00:00:00' AND time < '2012-11-01 00:00:00'
+----+-------------+-------------+------+----------------------+------+---------+------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------+------+----------------------+------+---------+------+--------+-------------+
| 1 | SIMPLE | log | ALL | log_time_idx | NULL | NULL | NULL | 481300 | Using where |
+----+-------------+-------------+------+----------------------+------+---------+------+--------+-------------+
你知道问题是什么吗?我使用的是Mysql 5.1.48。
答案 0 :(得分:0)
MySQL似乎讨厌具有高基数的范围。在使用DATETIME
值时,这会很快显示出来。
您可以通过在表中为查询添加DATE
列来减少基数。基数将低得多,MySQL将更有可能使用索引。