gpt4 book ai didi

mysql - 优化MySQL查询:以下

转载 作者:行者123 更新时间:2023-11-29 13:24:45 25 4
gpt4 key购买 nike

我是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;

最佳答案

根据您提供的有限信息,我将尽可能扩展您的可能性(如果您还没有这样做)

首先,让我们重写您的查询:

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_pendingi.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

关于mysql - 优化MySQL查询:以下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20297070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com