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

标签 mysql

我正在尝试运行查询以将数据一次从客户端数据库获取到我们的数据库,但当我从主键 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/

相关文章:

mysql - SQL 对表之间共享值的行求和

MySQL JOIN 两张表

java - 如何将从数据库中检索到的字符串值与程序中定义的字符串值进行比较

MySQL:除 root 用户外,用户 'test' @'localhost' 的访问被拒绝(使用密码:YES)

mysql - 由于Join太长(MySQL),我们可以使用嵌套查询来代替Join吗?

php - MySQL 语句需要 - 选择每个用户最近的消息

mysql - 计算MySQL中的时间差

MySQL 在创建 MySQL-Events 时出错

C# winform 从 DataTable 中设置组合框项的值和名称

php - Codeigniter 事件记录 LIKE 查询