我是mysql的新手,我必须减少以下更新查询的执行时间
UPDATE temp_countcalculations,
(
SELECT count(*) as insuffcounts,CRP_RefNo as ref
FROM testsymphony7.p_education
WHERE testsymphony7.p_education.EducationStatusId=6
GROUP BY CRP_RefNo
) as icounts
SET Edu_pending=icounts.insuffcounts
WHERE temp_countcalculations.crp_refno=icounts.ref;
答案 0 :(得分:1)
根据您提供的有限信息,我会尽可能地扩展您的可能性(如果您还没有这样做)
首先,让我们重写您的查询:
UPDATE
temp_countcalculations t
JOIN (
SELECT
COUNT(*) as insuffcounts,
CRP_RefNo as ref
FROM
testsymphony7.p_education p
WHERE
p.EducationStatusId = 6
GROUP BY
CRP_RefNo
) i ON t.crp_refno = icounts.ref
SET
t.Edu_pending = i.insuffcounts;
好。
因此,您根据他们的参考,使用t.Edu_pending
更新所有i.insuffcounts
。
这里有2个要优化的查询。
(1):
SELECT
COUNT(*) AS insuffcounts,
CRP_RefNo as ref
FROM
testsymphony7.p_education p
WHERE
p.EducationStatusId = 6
GROUP BY
CRP_RefNo
和(2):
SELECT 1
FROM
temp_countcalculations t
JOIN ((1)) i ON t.crp_refno = icounts.ref
优化(1):
CRP_RefNo, EducationStatusId
(复合)testsymphony7.p_education.crp_refno
NOT NULL
,如果可能,UNIQUE
。优化(2):
temp_countcalculations.crp_refno
temp_countcalculations.crp_refno
NOT NULL
,如果可能,UNIQUE
。基于此,我们可能会更进一步了解您的结果:
EXPLAIN
SELECT 1
FROM
temp_countcalculations t
JOIN (
SELECT
COUNT(*) as insuffcounts,
CRP_RefNo as ref
FROM
testsymphony7.p_education p
WHERE
p.EducationStatusId = 6
GROUP BY
CRP_RefNo
) i ON t.crp_refno = icounts.ref