优化以下MySQL查询:
我是mysql新手,我必须减少以下更新查询的执行时间优化以下MySQL查询:,mysql,Mysql,我是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_
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;
好
因此,您正在根据它们的引用,使用i.inufffcounts
更新所有t.Edu_pending
。
这里有两个查询需要优化
(1) :
及(二):
优化(1):
- 列上的理想索引:
(复合)CRP\u参考号,EducationStatusId
- 列
testsymphony7.p\u education.crp\u refno
,如果可能,不为空
唯一
- 列的理想索引
temp\u countcalculations.crp\u refno
- 列
temp\u countcalculations.crp\u refno
,如果可能,不为空
唯一
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
您得到的错误是什么?更新温度计算。temp后面的逗号是什么_countcalculations@AjeetManral,这是一个隐式联接。请添加
SHOW CREATE TABLE temp\u countcalculations
和SHOW CREATE TABLE testsymphony7.p\u education
hi@AjeetManral的结果,此查询的执行时间太长,我必须减少时间,所以我正在努力寻找减少时间的方法。由于我是mysql新手(用于在sql server中编写代码),我正在尝试找到一种更快更新查询的方法。。正如我上面提到的更新查询,由于您描述的列被索引,它花费了太多的时间……在sql server中,如果我必须更新计算的计数,我不需要使用group by查询,但在mysql中,情况并非如此working@SanketJoshi_StackOver,我需要我在结果中提到的2个SHOW CREATE TABLE
语句以及我在问题中提出的EXPLAIN
语句。temp\u countcalculations,CREATE TABLEtemp\u countcalculations
(nCrp\u Refno
varchar(50)字符集utf8 NOT NULL,nEdu Pending
int(11)默认0,nEmp_未决
int(11)默认0,nResi_未决
int(11)默认0,ncrim_未决
int(11)默认0,nref_未决
int(11)默认0,nential_未决
int(11)默认0,ncourt_未决
int(11)默认0,n数据库检查
int(11)默认0,nIdentity\u Pending
int(11)默认0,nTotal\u Pending
int(11)默认0,nTotal\u Check
int(11)默认0n)ENGINE=InnoDB DEFAULT CHARSET=latin1p\u education,创建表p\u education
(nCRP\u RefNo
varchar(50)不为空,nEducationID
int(11)不为空,nEducationPassingYear\u Auto
日期默认为空,nReportId
int(11)默认为空,nSubstanceCheckId
int(11)默认为0,n主键(CRP\u RefNo
,EducationID
),n键FK\u ReportId教育
(ReportId
),n约束FK_ReportIdEducation
外键(ReportId
)引用m_reporttype
(ReportId
)关于删除的无操作关于更新的无操作n)引擎=InnoDB默认字符集=1
SELECT 1
FROM
temp_countcalculations t
JOIN ((1)) i ON t.crp_refno = icounts.ref
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