简体   繁体   English

使用MySQL EXPLAIN优化查询

[英]Optimize Query with MySQL EXPLAIN

I'm a bit of a db noob and have a nasty query that is taking over 30 seconds to run. 我有点数据库无聊,并有一个讨厌的查询,需要30秒钟才能运行。 I'm trying to learn a bit more about EXPLAIN and optimize the query but am at a loss. 我试图学习有关EXPLAIN的更多信息并优化查询,但无所适从。 Here is the query: 这是查询:

SELECT 
    feed.*, users.username, smf_attachments.id_attach AS avatar, 
    games.name AS item_name, games.image, feed.item_id, u2.username AS follow_name
FROM feed
INNER JOIN following ON following.follow_id = feed.user_id AND following.user_id = 1
LEFT JOIN users ON users.id = feed.user_id
LEFT JOIN smf_members ON smf_members.member_name = users.username
LEFT JOIN smf_attachments ON smf_attachments.id_member = smf_members.id_member
LEFT JOIN games ON games.id = feed.item_id
LEFT JOIN users u2 ON u2.id = feed.item_id
ORDER BY feed.timestamp DESC
LIMIT 25

Explain results: 说明结果:

The result you will want to avoid in your execution plan (the output of an explain statement) is "full scan" (extra field of the explain output). 您将要在执行计划中避免的结果(explain语句的输出)是“ full scan”(解释输出的额外字段)。 In order to avoid it, you need to create the correct indexes on your tables. 为了避免这种情况,您需要在表上创建正确的索引。

If you have a table scan, it means the query engine read sequentially each row of the the table. 如果进行表扫描,则意味着查询引擎按顺序读取表的每一行。 Instead, with index access, the query engines accesses more directly the relevant data. 而是使用索引访问,查询引擎可以更直接地访问相关数据。

More explanation here: http://dev.mysql.com/doc/refman/5.0/en/using-explain.html 此处提供更多说明: http : //dev.mysql.com/doc/refman/5.0/en/using-explain.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM