基本ORDER BY查询的INDEX

时间:2013-08-02 20:45:09

标签: mysql indexing

我对整个INDEXING事情有点新意,但到目前为止我很喜欢它!只是一个快速(可能非常简单)的问题......我有一个非常基本的查询,我想从“评论”表中只选择最近的5条评论。 “id”列是PRIMARY。

mysql> EXPLAIN SELECT * FROM comments ORDER BY id DESC LIMIT 5;
+----+-------------+----------+-------+---------------+---------+---------+------+-------+-------+
| id | select_type | table    | type  | possible_keys | key     | key_len | ref  | rows  | Extra |
+----+-------------+----------+-------+---------------+---------+---------+------+-------+-------+
|  1 | SIMPLE      | comments | index | NULL          | PRIMARY | 4       | NULL | 19938 |       |
+----+-------------+----------+-------+---------------+---------+---------+------+-------+-------+
1 row in set (0.00 sec)

正如您所看到的,它正在扫描整个桌子......有什么方法可以解决这个问题吗?

更新SHOW CREATE TABLE注释:

mysql> show create table comments;
+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table     |
+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| comments | CREATE TABLE `comments` (
  `id` int(12) NOT NULL auto_increment,
  `post_id` int(12) NOT NULL,
  `comm_id` int(11) NOT NULL,
  `userid` int(12) NOT NULL,
  `name` varchar(30) NOT NULL,
  `content` text NOT NULL,
  `location` varchar(30) NOT NULL,
  `type` enum('Comment','Advice') NOT NULL,
  `likes` int(12) NOT NULL,
  `dislikes` int(12) NOT NULL,
  `datetime` varchar(255) NOT NULL,
  `datetimerss` varchar(255) NOT NULL,
  `ip` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `type` (`type`)
) ENGINE=MyISAM AUTO_INCREMENT=20091 DEFAULT CHARSET=latin1 |
+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

0 个答案:

没有答案