查询:
SELECT `files`.*, `file_status`.`downloaders` FROM `files`
INNER JOIN `file_status` ON `files`.file_id = `file_status`.file_id
WHERE `files`.type`> 0 AND `files`.type` <= 2
ORDER BY `downloaders` DESC
LIMIT 50表:
CREATE TABLE IF NOT EXISTS `files` (
`file_id` int(11) NOT NULL,
`name` varchar(150) COLLATE utf8_unicode_ci NOT NULL,
`type` int(11) NOT NULL,
`description` text COLLATE utf8_unicode_ci NOT NULL,
`user` varchar(35) COLLATE utf8_unicode_ci NOT NULL,
`size` bigint(20) NOT NULL,
`upload_time` int(11) NOT NULL,
PRIMARY KEY (`file_id`),
KEY `type` (`type`),
FULLTEXT KEY `name_full` (`name`),
FULLTEXT KEY `description` (`description`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE IF NOT EXISTS `file_status` (
`file_id` int(11) NOT NULL,
`downloaders` int(11) NOT NULL,
`complete` int(11) NOT NULL,
`last_update` int(11) NOT NULL,
PRIMARY KEY (`file_id`),
KEY `downloaders` (`downloaders`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;此查询在表中的200 k项上执行5秒。表稍后会有更多的条目,所以我担心这将是一场彻底的灾难。
如果我按照正常执行的方式删除命令。
除了将列移动到第一个表之外,还有其他方法来加快速度吗?
解释:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE files ALL PRIMARY,type NULL NULL NULL 256956 Using where; Using temporary; Using filesort
1 SIMPLE file_status eq_ref PRIMARY PRIMARY 4 x.files.file_id 1 NULL发布于 2015-01-08 15:25:41
极不愉快的:
SELECT * FROM `files` AS x INNER JOIN (
SELECT `file_id`, downloaders FROM `file_status` WHERE `file_id` IN (
SELECT `file_id` FROM `files` WHERE `type`>0 AND `type`<=2
) ORDER BY `downloaders` DESC LIMIT 50
) as y ON x.`file_id` = y.`file_id`
INNER JOIN `file_status` ON x.`file_id` = `file_status`.`file_id`第一个内部联接是因为否则mysql表示它不支持子查询中的限制
我们对每个表使用子查询,使mysql使用正确的索引(键)。
这立即执行。
https://stackoverflow.com/questions/27842439
复制相似问题