以下查詢需要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