【发布时间】:2011-10-10 21:04:44
【问题描述】:
我正在使用以下查询来获取最新文章(文章标题+作者姓名+总cmets),但是运行时间很长:
SELECT article.id, title, username, count( comment.id ) AS total_comments
FROM article
LEFT JOIN COMMENT ON comment.article_id = article.id
LEFT JOIN user ON article.user_id = user.id
WHERE STATUS = "open"
AND section = "mobiles"
GROUP BY article.id
ORDER BY article.id DESC
LIMIT 0 , 30
解释的输出是:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE article ALL status NULL NULL NULL 77 Using where; Using temporary; Using filesort
1 SIMPLE comment ref article_id article_id 5 test.article.id 2
1 SIMPLE user eq_ref PRIMARY PRIMARY 4 test.article.user_id 1
如何重写查询以避免创建临时表?
存储引擎是 MyISAM。这是带有索引的表的详细信息:
article
id, title, body, user_id, status, section
primary key: id
indexes: (user_i),(status,section,id)
comment
id, article_id, user_id, body
primary key: id
index:(article_id)
user
id, username
primary key: id
【问题讨论】:
-
有多长?表中有多少行?
-
400k 行大约需要 14 秒。上面的例子需要 0.5 秒。如果您从查询中删除评论连接和计数,则需要 0.025 秒。