0

I am new to mysql, I have to reduce the execution time of below update query

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;
5
  • what is the error you are getting? Commented Nov 30, 2013 at 5:53
  • UPDATE temp_countcalculations , . what is this comma after temp_countcalculations Commented Nov 30, 2013 at 5:54
  • 1
    @AjeetManral, this is an implicit JOIN. Commented Nov 30, 2013 at 5:55
  • Please add the result of SHOW CREATE TABLE temp_countcalculations AND SHOW CREATE TABLE testsymphony7.p_education Commented Nov 30, 2013 at 5:56
  • hi @AjeetManral , the execution time of this query is too long, i have to reduce the time so i wqas trying to find ways how it can be reduced.. Commented Nov 30, 2013 at 8:46

1 Answer 1

1

Based on the limited informations you provided, I'll expand as good as I can the possibilities you have (if you did not already do it)

First, let's rewrite your query:

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;

Good.

So, you're updating all the t.Edu_pending with i.insuffcounts, based on their reference. There are 2 queries to optimize here.

(1):

SELECT 
    COUNT(*) AS insuffcounts,
    CRP_RefNo as ref
FROM 
    testsymphony7.p_education p
WHERE 
    p.EducationStatusId = 6 
GROUP BY 
    CRP_RefNo

and (2):

SELECT 1 
FROM
    temp_countcalculations t
        JOIN ((1)) i ON t.crp_refno = icounts.ref

Optimizing (1):

  • Ideal index on columns: CRP_RefNo, EducationStatusId (composite)
  • Column testsymphony7.p_education.crp_refno NOT NULL and if possible, UNIQUE.

Optimizing (2):

  • Ideal index on columns temp_countcalculations.crp_refno
  • Column temp_countcalculations.crp_refno NOT NULL and if possible, UNIQUE.

Based on that, we might be able to go a bit further with your result of:

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 
Sign up to request clarification or add additional context in comments.

6 Comments

as i am new to mysql (use to code in sql server), i am trying to find a way to update query faster.. as i mentioned update query above it is taking too much time as you described columns are indexed...
in sql server i do not need to use group by the query if i have to update calculated counts but here in mysql same was not working
@SanketJoshi_StackOver, I need the 2 SHOW CREATE TABLE statements I mentionned in the results as well as the EXPLAIN I'm asking in the question, please.
temp_countcalculations, CREATE TABLE temp_countcalculations (n Crp_Refno varchar(50) CHARACTER SET utf8 NOT NULL,n Edu_Pending int(11) DEFAULT 0,n Emp_Pending int(11) DEFAULT 0,n Resi_Pending int(11) DEFAULT 0,n crim_Pending int(11) DEFAULT 0,n ref_Pending int(11) DEFAULT 0,n genuine_Pending int(11) DEFAULT 0,n court_Pending int(11) DEFAULT 0,n database_check_Pending int(11) DEFAULT 0,n Identity_Pending int(11) DEFAULT 0,n Total_Pending int(11) DEFAULT 0,n Total_Check int(11) DEFAULT 0n) ENGINE=InnoDB DEFAULT CHARSET=latin1
p_education, CREATE TABLE p_education (n CRP_RefNo varchar(50) NOT NULL,n EducationID int(11) NOT NULL,n EducationPassingYear_Auto date DEFAULT NULL,n ReportId int(11) DEFAULT NULL,n SubordinateCheckId int(11) DEFAULT 0,n PRIMARY KEY (CRP_RefNo,EducationID),n KEY FK_ReportIdEducation_idx (ReportId),n CONSTRAINT FK_ReportIdEducation FOREIGN KEY (ReportId) REFERENCES m_reporttype (ReportId) ON DELETE NO ACTION ON UPDATE NO ACTIONn) ENGINE=InnoDB DEFAULT CHARSET=latin1
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.