我有一个具有这种结构的表(有 50k 个字段):
CREATE TABLE IF NOT EXISTS `comments` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`imageid` int(10) unsigned NOT NULL DEFAULT '0',
`uid` bigint(20) unsigned NOT NULL DEFAULT '0',
`content` text CHARACTER SET utf8,
`adate` datetime DEFAULT NULL,
`ip` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `ids` (`imageid`,`adate`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=52236 ;
我想通过 imageid 选择数据并使用 adate 对其进行排序,所以我添加了 ( imageid
, adate
) 键。
但是这个查询的解释结果说 MuSQL 仍然使用表扫描。为什么?!
EXPLAIN SELECT comments.*
FROM comments
WHERE comments.imageid=50
ORDER BY
comments.adate DESC LIMIT 10
结果:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE comments ref ids ids 4 const 203 Using where
并使用此索引:
KEY `ids` (`imageid`,`adate`,`id`) USING BTREE
此查询的结果:
EXPLAIN SELECT comments.id
FROM comments
WHERE comments.imageid=50
ORDER BY
comments.adate DESC LIMIT 10
是:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE comments ref ids ids 4 const 203 Using where; Using index