Tengo una pregunta muy simple:Mi sencilla consulta MySQL no utilizar el índice
SELECT comments.*
FROM comments
WHERE comments.imageid=46
y este es mi tabla:
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 ;
Pero MySQL no puede utilizar índice en esta consulta sencilla . aquí está el resultado de la explicación:
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
mientras cambio la consulta a esto, Mysql puede usar el índice. ¿Por qué? :
SELECT comments.id
FROM comments
WHERE comments.imageid=46
aquí es la explique:
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
No es * "Pero MySql ** no puede ** usar el índice" *. Es: "Pero MySql ** no ** usará el índice". Las 'posibles_keys = ids' y' key = NULL' sugieren que el uso del índice fue examinado por el optimizador y rechazado. Se pensó que la exploración completa de la tabla era más rápida, y probablemente sea en su caso. –