以下查询在大约 50 万行的表上执行需要 20 多秒:
SELECT images.id, images.user_id, images_locale.filename, extension, size, width, height, views, batch, source, status, images.created_at, images.category_id, title, short_description, long_description, alternate, slugs.name as slug, images_locale.slug_id, path_cache AS category_path, full_name, users.username
FROM images
JOIN images_locale ON images_locale.image_id = images.id JOIN slugs ON images_locale.slug_id = slugs.id JOIN categories_locale ON images.category_id = categories_locale.category_id JOIN users ON users.id = images.user_id
WHERE slugs.name = 'THE_SLUG_HERE' AND images.status = '1' AND images_locale.locale_id = 1 AND categories_locale.locale_id = 1
LIMIT 1
现在,当我删除时,slugs.name = 'THE_SLUG_HERE' AND
我会在几毫秒内得到结果。
这是我的 slug 表:
CREATE TABLE `slugs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(250) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '',
`type` tinyint(4) NOT NULL,
`locale_id` smallint(6) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=3611900 DEFAULT CHARSET=utf8;
我尝试过,CREATE INDEX test_speed ON slugs(name)
但它并没有加快速度。
请帮忙。
编辑:
以下是 EXPLAIN 的结果: