我正在尝试运行查询以将数据一次从客户端数据库获取到我们的数据库,但当我从主键 user_appoint.id< 更改顺序时,执行查询需要花费大量时间
到 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;
user_appoint.u_id
已正确索引。
最佳答案
请检查您的查询的解释计划。最好始终与您最初的问题分享解释计划。
explain format=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
,'"')),"]") 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;
关于mysql - 查询需要花费大量时间来执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46557418/