查询需要花费大量时间来执行

时间:2017-10-04 05:09:32

标签: mysql

我正在尝试运行查询以从客户端数据库一次获取数据到我们的数据库,但是当我将订单从主键user_appoint.id更改为{时,查询需要花费大量时间来执行下面的{1}}是我的查询

user_appoint.u_id

SELECT CONCAT('D',user_appoint.`id`) AS ApptId, user_appoint.`u_id`, tbl_questions.CandAns, tbl_questions.ExamAns, tbl_questions.QueNote, CONCAT("[",GROUP_CONCAT(CONCAT('"',`tbl_investigations`.`test_id`,'":"',tbl_investigations.`result`,'"')),"]") AS CandInv, CONCAT("[",GROUP_CONCAT(CONCAT('"',`tbl_investigations`.`test_id`,'":"',tbl_investigations.`comments`,'"')),"]") AS IntComm, IF(tbl_questions.LastUpdatedDateTime>MAX(tbl_investigations.`ModifiedAt`),tbl_questions.LastUpdatedDateTime,MAX(tbl_investigations.`ModifiedAt`)) AS LastUpdatedDateTime, CONCAT('D',user_appoint.`id`) AS UniqueId FROM user_appoint LEFT JOIN tbl_investigations ON tbl_investigations.`appt_id`=user_appoint.`id` AND tbl_investigations.`ModifiedAt`>'2011-01-01 00:00:00' LEFT JOIN tbl_questions ON tbl_questions.`appt_id` =user_appoint.`id` AND tbl_questions.`LastUpdatedDateTime`>'2011-01-01 00:00:00' GROUP BY user_appoint.`id` HAVING LastUpdatedDateTime>'2011-01-01 00:00:00' ORDER BY user_appoint.`u_id` LIMIT 0, 2000; 已正确编入索引。

3 个答案:

答案 0 :(得分:0)

请查看您的查询的解释计划。最好总是与原始问题分享解释计划。

  

解释格式= json

     

SELECT CONCAT('D',user_appoint。id)AS ApptId,user_appoint。u_id,   tbl_questions.CandAns,tbl_questions.ExamAns,tbl_questions.QueNote,   CONCAT( “[”,GROUP_CONCAT(CONCAT( ' “',tbl_investigations test_id,。 ' ”:“'。,tbl_investigations result, '”')), “]”)   作为CandInv,   CONCAT( “[”,GROUP_CONCAT(CONCAT( ' “',tbl_investigations test_id,。 ' ”:“'。,tbl_investigations comments, '”')), “]”)   AS IntComm,   IF(tbl_questions.LastUpdatedDateTime> MAX(tbl_investigations ModifiedAt),tbl_questions.LastUpdatedDateTime,MAX(tbl_investigations ModifiedAt))   AS LastUpdatedDateTime,CONCAT('D',user_appoint。id)AS UniqueId FROM   user_appoint LEFT JOIN tbl_investigations ON   tbl_investigations。appt_id = user_appoint。id AND   tbl_investigations。ModifiedAt>'2011-01-01 00:00:00'LEFT JOIN   tbl_questions ON tbl_questions。appt_id = user_appoint。id AND   tbl_questions。LastUpdatedDateTime>'2011-01-01 00:00:00'GROUP BY   user_appoint。id已使用LastUpdatedDateTime>'2011-01-01 00:00:00'   ORDER BY user_appoint。u_id LIMIT 0,2000;

答案 1 :(得分:0)

在查看您的查询时,我可以看到很多concat,聚合函数和连接正在单个查询中执行。

由于您已对查询执行设置了限制,因此将对所有2000条记录执行这些操作。 这可能导致查询减慢其执行速度。

答案 2 :(得分:0)

您有2个具有不同别名的相同列

CONCAT('D',user_appoint.`id`) AS ApptId,
CONCAT('D',user_appoint.`id`) AS UniqueId

(已更改)假设这些日期列中可能出现NULL,则比较max()值将克服NULL的任何不利影响:

if(max(tbl_questions.lastupdateddatetime) > max(tbl_investigations.`modifiedat`) , max(tbl_questions.lastupdateddatetime), max(tbl_investigations.`modifiedat`))  AS LastUpdatedDateTime

试试这个:

SELECT *
FROM (
    SELECT
            Concat('D', user_appoint.`id`)  AS ApptId
          , user_appoint.`u_id`
          , tbl_questions.candans
          , tbl_questions.examans
          , tbl_questions.quenote
          , Concat("[", Group_concat(Concat('"', `tbl_investigations`.`test_id`, '":"', tbl_investigations.`result`, '"')), "]") AS CandInv
          , Concat("[", Group_concat(Concat('"', `tbl_investigations`.`test_id`, '":"', tbl_investigations.`comments`, '"')), "]") AS IntComm
          , if(max(tbl_questions.lastupdateddatetime) > max(tbl_investigations.`modifiedat`) , max(tbl_questions.lastupdateddatetime), max(tbl_investigations.`modifiedat`) )  AS LastUpdatedDateTime
          , Concat('D', user_appoint.`id`) AS UniqueId 
    FROM   user_appoint 
           LEFT JOIN tbl_investigations 
                  ON tbl_investigations.`appt_id` = user_appoint.`id` 
                     AND tbl_investigations.`modifiedat` > '2011-01-01 00:00:00' 
           LEFT JOIN tbl_questions 
                  ON tbl_questions.`appt_id` = user_appoint.`id` 
                     AND tbl_questions.`lastupdateddatetime` > '2011-01-01 00:00:00' 
    GROUP  BY user_appoint.`id` 
    HAVING lastupdateddatetime > '2011-01-01 00:00:00' 
    ) d
ORDER  BY `u_id` 
LIMIT  0, 2000
; 

<强>无论其

您正在使用非当前和非标准形式的GROUP BY子句。 MySQL开始生活,允许这种奇怪的情况,你可以选择许多列,但只能按其中一个列分组。这对于SQL来说完全不标准。

在MySQL的最新版本中,默认设置已更改,并且在GROUP BY子句中仅使用一列将导致错误。

因此,您可能需要将执行分组的方式更改为

GROUP  BY
        user_appoint.`id`
      , user_appoint.`u_id`
      , tbl_questions.candans
      , tbl_questions.examans
      , tbl_questions.quenote

如果这些都没有提高性能,请提供执行计划(作为文本)。