我有以下 MSQL 查询,速度非常慢
SELECT a.id, a.date, a.Title, a.Text, a.Text2, a.Text3,
b.xxx, b.yyyy,
c.aaaa, c.bbbb
FROM table_a a
JOIN table_b b on a.Id = b.Id
JOIN table_c c on a.eeee = c.id
ORDER BY a.date DESC
LIMIT 200
table_a 的行数超过 100.000 行。我需要倒序排列最后 200 行。 MySQL所做的就是选择所有行,对所有行进行JOIN,然后反转全表,最后选择200条记录。整个过程大约需要30-40秒。
问题:由于table_a是按a.id(它是一个渐进的数字)和日期(新文章总是添加在最后一个位置)排序的,是否可以重写查询,以便仅在a.id上执行JOIN最新的 200 条记录而不是完整的表?这样查询就会快很多!
一般来说,如何优化??
你可以尝试一下,但我不确定是否可以 限制是允许的。
SELECT a.id, a.date, a.Title, a.Text, a.Text2, a.Text3,
b.xxx, b.yyyy,
c.aaaa, c.bbbb
FROM (
SELECT
a.id, a.date, a.Title, a.Text, a.Text2, a.Text3
FROM table_a
ORDER BY a.date DESC
LIMIT 200
) as a
JOIN table_b b on a.Id = b.Id
JOIN table_c c on a.eeee = c.id;