简体   繁体   English

MySQL中使用联接的查询缓慢

[英]Slow query in MySQL with joins

I have a query that runs too slow but I can't figure out the reason behind it, or a solution to it. 我的查询运行太慢,但无法弄清楚其背后的原因或解决方案。 The query took around 22 sec to run at first, but then I tried to remove a foreign key in the table Book that is a FK to ParentID in the same table. 该查询最初大约需要22秒才能运行,但是后来我尝试删除表Book中的外键,该表是同一表中对ParentID的FK。 After this removal the query took around 1 sec . 删除之后,查询耗时约1秒钟 This is of course good but there should be room for even more improvements, either restructure the query itself or by using different indexes. 这固然很好,但是应该有更多改进的余地,要么重组查询本身,要么使用不同的索引。

Can anyone advise? 有人可以建议吗? Why does the foreign key to Book.ParentID cause such an increase in query time? 为什么Book.ParentID的外键会导致查询时间增加呢? What is the best approach here, try to rewrite the query or look into the indexes? 这里最好的方法是什么,尝试重写查询或查看索引?

Except for dropping the foreign key to Book.ParentID , I've tried using use index on the selected table and some of the joins but with no real luck. 除了将外键放到Book.ParentID ,我尝试在所选表和某些联接上使用use index ,但没有真正的运气。

Here's a fiddle: http://sqlfiddle.com/#!9/7f7bbb/9 这是一个小提琴: http ://sqlfiddle.com/#!9/ 7f7bbb/9

However, the explain plan in the fiddle differ from the one I get in production. 但是,小提琴中的解释计划与我在生产中得到的解释计划有所不同。 This is the explain plan from production (with the foreign key to Book.ParentID): 这是从生产开始的解释计划(带有Book.ParentID的外键):

'1','SIMPLE','Bk',NULL,'index_merge','PRIMARY,fk_CategoryParent_ID_idx','PRIMARY,fk_CategoryParent_ID_idx','8,9',NULL,'2','100.00','Using union(PRIMARY,fk_CategoryParent_ID_idx); Using where; Using temporary; Using filesort'
'1','SIMPLE','CGB2',NULL,'ref','uq_ChapterGroup_Category,book_chaptergroup_unique_row_constraint,fk_Category_ChapterGroup_ID,ix_chapterGroupBook_chapterGroupID','book_chaptergroup_unique_row_constraint','9','Hypo.Bk.ID','8','100.00','Using where'
'1','SIMPLE','CCG2',NULL,'ref','uq_ChapterID_ChapterGroupItemInChapterGroup,uq_ExamID_ChapterGroupItemInChapterGroup,fk_ChapterGroup_Chapter_ID,ix_chapterChapterGroup_chapterID','fk_ChapterGroup_Chapter_ID','8','Hypo.CGB2.ChapterGroupID','2','100.00','Using where'
'1','SIMPLE','ATM',NULL,'ref','PRIMARY,fk_Chapter_AnswerTextMarker_ID,fk_QuestionGroup_AnswerTextMarker_ID','fk_Chapter_AnswerTextMarker_ID','8','Hypo.CCG2.ChapterID','5','100.00','Using where'
'1','SIMPLE','QG',NULL,'eq_ref','PRIMARY','PRIMARY','8','Hypo.ATM.QuestionGroupID','1','100.00','Using index'
'1','SIMPLE','AQS',NULL,'ref','PRIMARY,fk_AQSession_AnswerTextMarker_ID,ix_answerQuestionSession_questionSessionID','fk_AQSession_AnswerTextMarker_ID','8','Hypo.ATM.ID','313','100.00',NULL
'1','SIMPLE','QS',NULL,'eq_ref','PRIMARY,fk_QSession_User_ID,ix_QuestionSession_userId_type_sessionDate','PRIMARY','8','Hypo.AQS.QuestionSessionID','1','5.00','Using where'

Row count in my environment: 我的环境中的行数:

AnswerQuestionSession 3992125 答案问题会话3992125
QuestionSession 367334 会话367334
AnswerTextMarker 9696 AnswerTextMarker 9696
QuestionGroup 18793 课题组18793
ChapterGroupItemInChapterGroup 42360 ChapterGroupItemInChapterGroup 42360
ChapterGroupBook 2297 第2297章
Book 378 第378章

ChapterGroupBook looks like a many-to-many mapping table. ChapterGroupBook看起来像一个多对多映射表。 If so, why are all the columns nullable? 如果是这样,为什么所有列都可以为空? Make them (and any others elsewhere) NOT NULL where appropriate. 使它们(以及其他任何其他地方)在适当的地方NOT NULL

Because of the above, that table has no PRIMARY KEY . 由于上述原因,该表没有PRIMARY KEY This is bad for InnoDB. 这对InnoDB不利。 Also see the my many:many tips . 另请参阅我的许多技巧

OR is a performance killer. OR是性能杀手。 Is there any way you could avoid it in (Bk.ID=551 OR Bk.ParentID=551) ? (Bk.ID=551 OR Bk.ParentID=551)有什么方法可以避免它? If necessary, us a UNION in a derived table: 如有必要,我们在派生表中使用UNION

FROM ( ( SELECT id FROM Book WHERE id = 551 )
       UNION ALL
       ( SELECT id FROM Book WHERE parent_id = 551 ) )
JOIN ...

("Index merge" was used, but is likely to be slower.) (使用了“索引合并”,但可能会慢一些。)

Please reformat the EXPLAIN so that I can see the Rows column clearly. 请重新格式化EXPLAIN以便我可以清楚地看到“ Rows列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM