我有一个非常简单的查询:
SELECT comments.*
FROM comments
WHERE comments.imageid=46
这是我的桌子:
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`) USING BTREE,
KEY `dt` (`adate`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=12 ;
但是 MySql 不能在这个简单的查询上使用索引。这是解释结果:
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE comments ALL ids NULL NULL NULL 4 75.00 Using where
当我将查询更改为此时,Mysql 可以使用 index. 为什么?:
SELECT comments.id
FROM comments
WHERE comments.imageid=46
这是解释:
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE comments ref ids ids 4 const 4 100.00 Using index