Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
优化以下MySQL查询:_Mysql - Fatal编程技术网

优化以下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_

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

因此,您正在根据它们的引用,使用
i.inufffcounts
更新所有
t.Edu_pending
。 这里有两个查询需要优化

(1) :

及(二):

优化(1):

  • 列上的理想索引:
    CRP\u参考号,EducationStatusId
    (复合)
  • testsymphony7.p\u education.crp\u refno
    不为空
    ,如果可能,
    唯一
优化(2):

  • 列的理想索引
    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 TABLE
temp\u countcalculations
(n
Crp\u Refno
varchar(50)字符集utf8 NOT NULL,n
Edu Pending
int(11)默认0,n
Emp_未决
int(11)默认0,n
Resi_未决
int(11)默认0,n
crim_未决
int(11)默认0,n
ref_未决
int(11)默认0,n
ential_未决
int(11)默认0,n
court_未决
int(11)默认0,n
数据库检查
int(11)默认0,n
Identity\u Pending
int(11)默认0,n
Total\u Pending
int(11)默认0,n
Total\u Check
int(11)默认0n)ENGINE=InnoDB DEFAULT CHARSET=latin1p\u education,创建表
p\u education
(n
CRP\u RefNo
varchar(50)不为空,n
EducationID
int(11)不为空,n
EducationPassingYear\u Auto
日期默认为空,n
ReportId
int(11)默认为空,n
SubstanceCheckId
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