慢 MySQL 查询 - 如何提高性能

Slow MySQL query - how to improve performance

我有以下查询:

SELECT `authors`.email, COUNT(articles.id),  SUM(stats.count_stats)
FROM articles
INNER JOIN authors ON articles.id = `authors`.article_id 
LEFT JOIN (
    SELECT article_id, SUM(count_cited) AS count_stats 
    FROM article_citations_stats 
    GROUP BY article_id) AS stats ON articles.id = stats.article_id
GROUP BY `authors`.email
HAVING SUM(stats.count_stats) > 10

表格:

authors has 200 000 rows
articles has 60 000 riws
article_citations_stats has 200 000 rows

查询速度极慢。关于如何提高性能的任何想法。

考虑在表上添加以下索引(如果它们还没有的话)

articles - (id) 
authors - (id,email)
article_citations_stats - (article_id,count_cited)

这应该会加快您的查询速度。

此外,您可以说明您要做什么,如果可能的话,我们会帮助您提出更快的查询。

只是一个技术问题(抱歉,应该在评论中,但因为这是我的第一个 post 我还没有足够的分数):作者 允许有更多 articlesarticles 有更多 authors

根据当前定义,我可以在 authors table 中看到 idarticle_id 不为空。所以如果:

  1. idauthorid 那么每个 author 都有一篇 article(可能与另一个 author 共享),我想你不会不需要加入 articles table,你可以直接从 authors 计算 articles_id table。您也可以直接使用它来加入 stats table.

  2. id 只是行的 id 那么实际上 author 可以拥有比 1 个更多的 articles,但是 authors table 可能会很大,重新设计它可能更好author_id 文章 table 中。然而,在这种方法中,一篇 article.

  3. 不能有更多的 authors
  4. 允许许多作者有更多的文章,反之亦然,这里需要桥梁table。然后使用 table 进行分组。作为 1. 不允许有更多的文章和 2. 不允许有更多的作者我会选择 3.