This SELECT query takes 180 seconds to finish

后端 未结 3 1638
[愿得一人]
[愿得一人] 2020-12-06 02:08

UPDATE:

Just to mention it on a more visible place. When I changed IN for =, the query execution time went from 180 down to 0.00008 seconds. Ridiculous speed differe

3条回答
  •  渐次进展
    2020-12-06 02:37

    You can use EXPLAIN to find out how is it possible for a query to execute so slow.

    MySQL does not really like nested subselects so probably what happens is that it goes and does sorts on disk to get min and max and fail to reuse results.

    Rewriting as joins would probably help it.

    If just looking for a quick fix try:

    SET @temp1 =     
      (
      SELECT MIN(IdQuestionLaw) 
      FROM question_law 
      WHERE IdQuestion = 236 AND IdQuestionLaw > 63
      )
    
    SET @temp2 = 
      (
      SELECT MAX(IdQuestionLawVersion) 
      FROM question_law_version 
      WHERE IdQuestionLaw = @temp1
      )
    
    SELECT IdLawVersionValidFrom 
    FROM question_law_version 
    WHERE IdQuestionLawVersion = @temp2
    

提交回复
热议问题