我的查询遇到一些困难,其中包含多个左连接以及按组和顺序组合。
text table和textdetails包含+ - 800k记录
复制表和copydetails包含+ - 200k记录
其他表格要小得多。
我为每个执行左连接的列都有外键。 我还在每个列上都有索引,我在其中执行where语句。 下面的MySQL查询仍然运行大约40秒。 离开Group By会有所改善。 抛弃Order By可以改善很多。
我做了一些研究,但我仍然对如何改进查询或索引感到困惑。
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
或许我应该多解释一下这种关系?
feed:text = 1:n
text:textdetails = 1:1
text:copy = 1:n
copy:domain = n:1
channel:domain n:1
答案 0 :(得分:0)
我会改变一些事情并更新查询以反映这一点。另外,对于索引。如果每列都有索引,但它们是单独的索引,那么WONT必然会对您有所帮助。如果可能,您需要复合(多个字段)索引以更好地匹配您的连接/标准和分组。
SELECT
*
FROM
feed f
JOIN text t
ON feed.feed_id = t.text_feed_id
LEFT JOIN textdetails td
ON t.text_id = td.textdetails_text_id
JOIN COPY c
ON t.text_id = c.copy_text_id
LEFT JOIN domain d
ON c.copy_domain_id = d.domain_id
LEFT JOIN domaincategory dc
ON c.copy_domain_id = dc.domaincategory_domain_id
AND c.copy_account_id = cd.domaincategory_account_id
JOIN channel ch
ON c.copy_account_id = ch.channel_account_id
AND c.copy_domain_id = ch.channel_domain_id
AND ch.channel_active = 1
WHERE
f.feed_account_id = 96
AND f.feed_flag_delete IS NULL
AND t.text_flag_delete IS NULL
AND c.copy_flag_delete IS NULL
AND c.copy_tracking_date_found IS NOT NULL
GROUP BY
c.copy_id
ORDER BY
c.copy_tracking_date_found DESC
LIMIT
50
Transitive Property
由于copy.copy_domain_id是domain.domain_id的连接,channel.channel_domain_id连接到domain.domain_id,我们只需更改为copy.copy_domain_id = channel.channel_domain_id,而不需要拆分连接到其他相同值的表。< / p>
第二个......你有LEFT JOINS,但是当你添加&#34; feed_account_id = 96&#34;时,你会自动将它转换为INNER JOIN,因为它是一个要求,因此使TEXT别名成为一个连接好。与具有Channel_Active = 1的CHANNEL表类似(我有更新查询以反映该情况)。
现在,由于根据特定帐户的FEED表进行限定,我已将其移至第一个FROM位置并连接到Copy表。
现在,索引有助于优化此
table index
feed ( feed_account_id, feed_id, feed_flag_delete )
text ( text_feed_id, text_id, text_flag_delete )
textdetails ( textdetails_text_id )
copy ( copy_text_id, copy_domain_id, copy_account_id, copy_flag_delete, copy_tracking_date_found, copy_id )
domain ( domain_id )
domaincategory ( domaincategory_domain_id, domaincategory_account_id )
channel ( channel_domain_id, channel_account_id, channel_active )