MySQL加入性能不佳

时间:2014-10-29 13:42:11

标签: mysql performance join

我正在尝试根据相同的taxonomy_index ID选择相关内容,但是却遇到了慢查询。

首先尝试(性能非常差):

    SELECT n.nid, n.created, title, 5 AS rel 
FROM taxonomy_index ti1 
    JOIN taxonomy_index ti2 ON ti1.tid = ti2.tid AND ti2.nid != 235884
    INNER JOIN node n on ti2.nid = n.nid 
WHERE ti1.nid = NAME_CONST('nid',235884) 
    AND (`type` = 'np_news' OR `type` = 'blog_indl_g') 
    AND n.created < UNIX_TIMESTAMP() 
GROUP BY ti2.nid HAVING count(*) = 3 
LIMIT 10;

Query 1 explained

第二次尝试(表现不佳):

SELECT n.nid, n.created, title, 5 AS rel  from taxonomy_index ti
    INNER JOIN node n on ti.nid = n.nid
WHERE tid IN (SELECT td.tid FROM taxonomy_index ti inner join taxonomy_term_data td on ti.tid = td.tid WHERE ti.nid = 297621 AND td.vid in (5,11) AND `name` != '')
    AND (`type` = 'np_news' OR `type` = 'blog_indl_g')  
    AND n.created < UNIX_TIMESTAMP() 
GROUP BY ti.nid
HAVING count(*) = 3
LIMIT 10;

Query 2 explained

嗯,我还是不知道为什么这么慢!子选择表现很好并返回3个tid:785,786,792。

第三次尝试:(表现良好):

SELECT n.nid, n.created, title, 5 AS rel  from taxonomy_index ti
    INNER JOIN node n on ti.nid = n.nid
WHERE tid IN (785,786,792)
    AND (`type` = 'np_news' OR `type` = 'blog_indl_g')  
    AND n.created < UNIX_TIMESTAMP() 
GROUP BY ti.nid
HAVING count(*) = 3
LIMIT 10;

第三个实际上似乎表现得相当不错,但它不能完成这项工作,因为id是静态的。 我该如何优化呢?

注意*我无法发布链接以解释第三个查询。

0 个答案:

没有答案