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.
The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:
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`);
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