我的查詢中遇到了一些困難,其中包含多個左連接以及一個group by和order。與多個左連接,按順序排列的mysql優化
文本表和textdetails包含+ - 800K記錄
複製表和copydetails包含+ - 200K記錄
其他表小了很多。
我有每個列的外鍵我執行一個左連接。 我也在每一列上執行where語句的索引。 下面的MySQL查詢仍然運行大約40秒。 退出羣組通過提高一點。 退出訂單改進了很多。
我做了一些研究,但我仍然對如何改進我的查詢或索引感到困惑。
SELECT * FROM `copy`
LEFT JOIN `domain` ON domain.domain_id = copy.copy_domain_id
LEFT JOIN `domaincategory` ON copy.copy_domain_id = domaincategory.domaincategory_domain_id AND domaincategory.domaincategory_account_id = copy.copy_account_id
LEFT JOIN `text` ON text.text_id = copy.copy_text_id LEFT JOIN `textdetails` ON textdetails.textdetails_text_id = text.text_id
LEFT JOIN `channel` ON channel.channel_domain_id = domain.domain_id AND channel.channel_account_id = copy.copy_account_id
LEFT JOIN `feed` ON feed.feed_id = text.text_feed_id
WHERE (feed.feed_account_id = 96) AND (feed.feed_flag_delete IS NULL) AND (text.text_flag_delete IS NULL) AND (copy.copy_flag_delete IS NULL) AND (copy.copy_tracking_date_found IS NOT NULL) AND (channel.channel_active = 1)
GROUP BY `copy`.`copy_id`
ORDER BY `copy`.`copy_tracking_date_found` DESC LIMIT 50
的EXPLAIN選項的結果如下所示,但我無法弄清楚如何閱讀並使用它正確
ID : 1
Select_type : SIMPLE
Table : Feed
Type : Ref
Possible_Keys: PRIMARY,fk_feed_account_id,feed_flag_delete
Key: fk_feed_account_id
Key_len : 4:
Ref : const
Rows : 1
Extra: Using where; Using temporary; Using filesort
ID : 1
Select_type : SIMPLE
Table : text
Type : Ref
Possible_Keys: PRIMARY,fk_text_feed_id,text_flag_delete
Key: text_flag_delete
Key_len : 2
Ref : const
Rows : 2628
Extra: Using where
ID : 1
Select_type : SIMPLE
Table : textdetails
Type : Ref
Possible_Keys: fk_textdetails_text_id
Key: fk_textdetails_text_id
Key_len : 5
Ref : text.text_id
Rows : 1
Extra:
ID : 1
Select_type : SIMPLE
Table : copy
Type : Ref
Possible_Keys: fk_copy_account_id,fk_copy_domain_id,fk_copy_text_...
Key: fk_copy_text_id
Key_len : 4
Ref : text.text_id
Rows : 1
Extra: Using where
ID : 1
Select_type : SIMPLE
Table : domain
Type : eq_ref
Possible_Keys: PRIMARY
Key: PRIMARY
Key_len : 4
Ref : copy.copy_domain_id
Rows : 1
Extra: Using where
ID : 1
Select_type : SIMPLE
Table : domaincategory
Type : eq_ref
Possible_Keys: fk_domaincategory_account_id,fk_domaincategory_dom
Key: fk_domaincategory_domain_id
Key_len : 4
Ref : domain.domain_id
Rows : 1
Extra:
ID : 1
Select_type : SIMPLE
Table : channel
Type : ref
Possible_Keys: fk_channel_account_id,fk_channel_domain_id,channel...
Key: fk_channel_domain_id
Key_len : 4
Ref : copy.copy_domain_id
Rows : 2
Extra: Using where
也許我應該解釋的關係多一點?
飼料:文本= 1:n的
文本:textdetails = 1:1
文本:複製= 1:n的
副本:域= N:1
信道:域中的N個:1
你需要有MySQL的解釋查詢做:https://dev.mysql.com/doc/refman/5.5/en/execution-plan -information.html – mikeb
爲什麼你不編輯你的問題併發布它們? – mikeb
我剛剛做到了。似乎無法弄清楚如何正確讀取EXPLAIN功能 – quibuz