使用许多LEFT JOIN提高SQL查询的性能

时间:2012-02-24 08:40:05

标签: mysql sql performance left-join

我有一个数据库结构,提供具有以下关联的新闻文章:

  • HABTM news_categories
  • HABTM标签
  • HABTM上传

我编写了一个SQL查询来将所有这些结合在一起:

 SELECT `news_articles`.*, 
 GROUP_CONCAT(DISTINCT tags.title) AS `tags`, 
 GROUP_CONCAT(DISTINCT tags.id) AS `tag_ids`,
 GROUP_CONCAT(DISTINCT news_categories.title) AS `news_categories`,
 GROUP_CONCAT(DISTINCT news_categories.id) AS `news_category_ids`,
 GROUP_CONCAT(DISTINCT news_categories.slug) AS `news_category_slugs`, 
 `news_articles_uploads`.`caption` AS `upload_caption`,
 `uploads`.`title` AS `upload_title`, 
 `uploads`.`basename` AS `upload_basename`,
 `uploads`.`extension` AS `upload_extension`,
 `uploads`.`path` AS `upload_path`
 FROM `news_articles`
 LEFT JOIN `news_articles_tags` ON news_articles_tags.news_article_id = news_articles.id
 LEFT JOIN `tags` ON news_articles_tags.tag_id = tags.id
 LEFT JOIN `news_articles_news_categories` ON news_articles_news_categories.news_article_id = news_articles.id
 LEFT JOIN `news_categories` ON news_articles_news_categories.news_category_id = news_categories.id
 LEFT JOIN `news_articles_uploads` ON (news_articles_uploads.news_article_id = news_articles.id AND news_articles_uploads.order = 0)
 LEFT JOIN `uploads` ON news_articles_uploads.upload_id = uploads.id 
 WHERE (news_categories.slug IN ("category-one","category-two","category-three","category-four","category-five")) AND (news_articles.published = 1)
 GROUP BY `news_articles`.`id`
 ORDER BY `news_articles`.`lead_article` DESC, `news_articles`.`created` DESC LIMIT 20;

问题是,当查询运行时,它很慢,并且在繁忙时段CPU使用率已经失控!

以下是上述查询的EXPLAIN(右键单击在新标签中打开以查看完整尺寸):

Explain result for the above query

您可以在此处找到架构:http://pastie.org/private/qoe2qo16rbqr5mptb4bug

服务器运行MySQL 5.1.55,网站使用Zend Framework执行查询和PHP 5.2.8。

我已经浏览了MySQL慢查询日志,并尽我所知添加了缺失的索引,但查询仍显示需要1-3秒才能执行。如果有人有任何想法,我会非常感激。提前谢谢。

2 个答案:

答案 0 :(得分:3)

您是否有news_articleslead_articlenews_articlescreated的索引,这是一个包含两列的索引。

create index news_articles_x1 on news_articles (lead_articles, created);

没有它,你将不会利用限制条款的顺序,它仍然会扫描整个表格并对其进行排序。

另外我会怀疑你是否需要一次爆炸中的所有这些数据?

答案 1 :(得分:3)

由于您的“WHERE”子句最初为您的新闻类别包含“AND” 一个指定的列表,它会强制连接到达INNER连接,而不是LEFT JOIN。 另外,我会尝试添加“STRAIGHT_JOIN”子句。这通常会迫使发动机 按照具体说明的顺序进行加入,而不是试图想到它自己 替代你...尤其是当其他表更多的是“查找”引用时。

我也会按照约旦的建议应用指数。

SELECT STRAIGHT_JOIN
      NA.*, 
      GROUP_CONCAT(DISTINCT tags.title) AS `tags`, 
      GROUP_CONCAT(DISTINCT tags.id) AS tag_ids,
      GROUP_CONCAT(DISTINCT NC.title) AS news_categories,
      GROUP_CONCAT(DISTINCT NC.id) AS news_category_ids,
      GROUP_CONCAT(DISTINCT NC.slug) AS news_category_slugs, 
      NAUp.`caption` AS upload_caption,
      Up1.`title` AS upload_title, 
      Up1.`basename` AS upload_basename,
      Up1.`extension` AS upload_extension,
      Up1.`path` AS upload_path
   FROM 
      news_articles NA
         INNER JOIN news_articles_news_categories NACats
            ON NA.id = NACats.news_article_id

            INNER JOIN news_categories NC
               ON NACats.news_category_id = NC.id
               AND NC.slug IN ( "category-one",
                                "category-two",
                                "category-three",
                                "category-four",
                                "category-five" )


         LEFT JOIN news_articles_tags NATags
            ON NA.ID = NATags.news_article_id

            LEFT JOIN tags
               ON NATags.tag_id = tags.id

         LEFT JOIN news_articles_uploads NAUp
            ON    NA.ID = NAUp.news_article_id 
              AND NAUp.order = 0

            LEFT JOIN uploads Up1
               ON NAUp.upload_id = Up1.id 

   WHERE 
      NA.Published = 1
   GROUP BY 
      NA.ID
   ORDER BY 
      NA.lead_article DESC, 
      NA.created DESC 
   LIMIT 20;