[Solved] Slow MySQL query - how to improve performance

EverSQL Database Performance Knowledge Base

Slow MySQL query - how to improve performance

Database type:

I have the following query:

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

Tables:

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

The query is extremely slow. Any idea on how to improve the performance.

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Avoid Subqueries (query line: 12): We advise against using subqueries as they are not optimized well by the optimizer. Therefore, it's recommended to join a newly created temporary table that holds the data, which also includes the relevant search index.
  2. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
  3. Explicitly ORDER BY After GROUP BY (modified query below): By default, the database sorts all 'GROUP BY col1, col2, ...' queries as if you specified 'ORDER BY col1, col2, ...' in the query as well. If a query includes a GROUP BY clause but you want to avoid the overhead of sorting the result, you can suppress sorting by specifying 'ORDER BY NULL'.
Optimal indexes for this query:
ALTER TABLE `article_citations_stats` ADD INDEX `article_stats_idx_article_id` (`article_id`);
ALTER TABLE `authors` ADD INDEX `authors_idx_article_id_email` (`article_id`,`email`);
The optimized query:
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_citations_stats.article_id,
                SUM(article_citations_stats.count_cited) AS count_stats 
            FROM
                article_citations_stats 
            GROUP BY
                article_citations_stats.article_id 
            ORDER BY
                NULL
        ) AS stats 
            ON articles.id = stats.article_id 
    GROUP BY
        `authors`.email 
    HAVING
        SUM(stats.count_stats) > 10 
    ORDER BY
        NULL

Related Articles



* original question posted on StackOverflow here.