3
我有一个多对多的查询,我想优化, 我应该为它创建什么样的索引?sql多对多查询索引优化
SELECT (SELECT COUNT(post_id)
FROM posts
WHERE post_status = 1) as total,
p.*,
GROUP_CONCAT(t.tag_name) tagged
FROM tags_relation tr
JOIN posts p ON p.post_id = tr.rel_post_id
JOIN tags t ON t.tag_id = tr.rel_tag_id
WHERE p.post_status=1
GROUP BY p.post_id
EXPLAIN
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY p ALL PRIMARY NULL NULL NULL 5 Using where; Using filesort
+1:相当多的反复试验,它取决于数据和它是如何访问... –
这里是结果 PRIMARY,post_status \t post_status 常量使用其中;使用filesort – dsportesa
现在,您的查询将post_status作为关键字,而不像之前的执行计划为NULL。 –