我的查询在哪里有问题Using filesort
,即使该字段已编入索引。
这是我的查询:
EXPLAIN
SELECT u.name, u.surname, u.id
FROM rw_users u
ORDER BY u.created DESC
LIMIT 4
这是输出:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE u ALL 9 Using filesort
这是create table
:
CREATE TABLE `rw_users` (
`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`created` int(11) unsigned NOT NULL,
`email` varchar(255) COLLATE utf8_swedish_ci NOT NULL,
`password` char(64) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`name` varchar(100) COLLATE utf8_swedish_ci NOT NULL,
`surname` varchar(100) COLLATE utf8_swedish_ci NOT NULL,
`premium` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT 'level or premium membership (0 = standard)',
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`),
KEY `password` (`password`),
KEY `created` (`created`),
KEY `premuim` (`premium`),
KEY `name_surname` (`name`,`surname`),
CONSTRAINT `FK_rw_users` FOREIGN KEY (`premium`) REFERENCES `rw_premium` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci
我需要做什么才能摆脱Using filesort
?我在具有 118 行的数据库(完全相同的表)上进行了测试,它仍然给我Using filesort
,所以它不能使用它,因为它比使用索引更快。