この構造のテーブルがあります(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でデータを選択し、日付で並べ替えたいので、(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