【发布时间】:2015-09-14 08:47:40
【问题描述】:
以下查询运行了 6.6 秒,产生了 26 行。
EXPLAIN 结果是两个“ref”类型的简单查询,使用键,扫描 23 和 48 行。
表 f 有 1000 行,表 m 有 42000 行。
seltype 表类型键 键 keylen 参考行 额外过滤 SIMPLE f ref PRIMARY, forum_site_id 4 const 23 100.00 使用 where;使用临时的;使用文件排序 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 使用 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 类型并定义为 INDEX。字段 forum_id 被定义为 FOREIGN KEY。
我很乐意找出导致这种令人发指的表现的原因:)
【问题讨论】:
-
应避免在第二张桌子上使用 where。
标签: mysql optimization explain