以下查询需要6.6秒来运行,并产生26行。使用EXPLAIN输出优化缓慢的MySQL查询
EXPLAIN结果是'ref'类型的两个SIMPLE查询,使用键扫描23行和48行。
表f有1000行,表m有42000行。
seltype table type keys key keylen ref rows filtered extra SIMPLE f ref PRIMARY, forum_site_id 4 const 23 100.00 Using where; Using temporary; Using filesort forum_site_id, forums_flag_list_new_posts SIMPLE m ref forum_msg_forum_id, forum_msg_forum_id 5 locali_db.f.id 48 100.00 Using where forum_msg_status, forum_msg_date
下面是该查询(相当简单的一个):
SELECT
m.id AS msg_id,
m.public_id AS msg_public_id,
more fileds of this table ...
f.id AS forum_id,
f.public_id AS forum_public_id,
more fileds of this table ...
FROM
forum_msgs m
INNER JOIN forums f ON
m.forum_id = f.id
WHERE
f.site_id = 19
AND f.flag_list_new_posts = 1
AND m.msg_date >= 1434803744
AND m.status <> 11
ORDER BY
m.msg_date DESC
LIMIT
100
在WHERE和ORDER BY子句是类型INTEGER的和被定义为索引中的所有字段。字段forum_id被定义为FOREIGN KEY。
我会很高兴,找出可能导致令人发指的性能:)
应该避免在第二张桌子上使用。 – Tim3880