我需要你的帮助来优化那些mysql查询,两者都在我的慢速查询日志中。MySQL查询优化JOIN
SELECT a.nom, c.id_apps, c.id_commentaire, c.id_utilisateur,
c.note_commentaire, u.nom_utilisateur
FROM comments AS c
LEFT JOIN apps AS a ON c.id_apps = a.id_apps
LEFT JOIN users AS u ON c.id_utilisateur = u.id_utilisateur
ORDER BY c.date_commentaire DESC LIMIT 5;
有上c.id_apps,a.id_apps,c.id_utilisateur,u.id_utilisateur和c.date_commentaire一个MySQL索引。
EXPLAIN结果:
id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra 1 | SIMPLE | c | index | NULL | date_commentaire | 8 | NULL | 119 | 1 | SIMPLE | a | eq_ref | PRIMARY | PRIMARY | 3 | c.id_apps | 1 | 1 | SIMPLE | u | eq_ref | PRIMARY | PRIMARY | 3 | c.id_utilisateur | 1 |
SELECT a.id_apps, a.id_itunes, a.nom, a.prix, a.resume, c.nom_fr_cat, e.nom_edit
FROM apps AS a
LEFT JOIN cat AS c ON a.categorie = c.id_cat
LEFT JOIN edit AS e ON a.editeur = e.id_edit
ORDER BY a.id_apps DESC LIMIT 20;
EXPLAIN结果:
id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra 1 | SIMPLE | a | index | NULL | PRIMARY | 3 | NULL | 5336 | 1 | SIMPLE | c | eq_ref | PRIMARY | PRIMARY | 1 | a.categorie | 1 | 1 | SIMPLE | e | eq_ref | PRIMARY | PRIMARY | 3 | a.editeur | 1 |
有上a.categorie,c.id_cat,a.editeur,E中的MySQL索引的。 id_edit and a.id_apps
谢谢
您可以发布查询计划吗?使用'EXPLAIN SELECT ...' – 2010-05-01 09:22:03
Pierre,不要尝试用手语法突出显示。只需将其格式化为代码(缩进四个空格)并将其格式化为好。在这种情况下我已经为你做了。 – 2010-05-01 09:29:27
各自的数据集有多大?我注意到没有'WHERE'子句;你真的想要所有的评论和应用程序返回? LIMIT'条款是设计的一部分,还是您在测试时添加它们? – 2010-05-01 09:34:18