我遇到了一个奇怪的 MySQL 索引问题。我有一张桌子views_video
:
CREATE TABLE `views_video` (
`video_id` smallint(5) unsigned NOT NULL,
`record_date` date NOT NULL,
`region` char(2) NOT NULL DEFAULT '',
`views` mediumint(8) unsigned NOT NULL
PRIMARY KEY (`video_id`,`record_date`,`region`),
KEY `video_id` (`video_id`)
)
该表包含 340 万条记录。
我EXPLAIN
在这个查询上运行:
SELECT video_id, views FROM views_video where video_id <= 156
我有:
+----+-------------+-------------+-------+------------------+----------+---------+------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------+-------+------------------+----------+---------+------+--------+-------------+
| 1 | SIMPLE | views_video | range | PRIMARY,video_id | video_id | 2 | NULL | 587984 | Using where |
+----+-------------+-------------+-------+------------------+----------+---------+------+--------+-------------+
但是当我EXPLAIN
在这个查询上运行时:
SELECT video_id, views FROM views_video where video_id <= 157
我有:
+----+-------------+-------------+------+------------------+------+---------+------+---------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------+------+------------------+------+---------+------+---------+-------------+
| 1 | SIMPLE | views_video | ALL | PRIMARY,video_id | NULL | NULL | NULL | 3412892 | Using where |
+----+-------------+-------------+------+------------------+------+---------+------+---------+-------------+
video_id
是从1到1034。156到157之间没有什么特别的。
这里会发生什么?
* 更新 *
我在数据库中添加了更多数据。现在video_id
是从 1 到 1064。表现在有 380 万条记录。差变成 114 和 115。