Mysql-how to optimize the execution of stored procedures?

Source: Internet
Author: User
Begin; DECLAREstarttimedatetime; # start time DECLAREendtimedatetime; # end time DECLAREsumStudyTimeINT; # DECLAREtargetStudyTimeINT at the current time; # duration at the target time; # t_stage_info stage 1 duration... mysql optimized the stored procedure

BEGIN
DECLARE personPlaneId BIGINT;

DECLARE starttime datetime; # start time
DECLARE endtime datetime; # end time
DECLARE sumStudyTime INT; # current time
DECLARE targetStudyTime INT; # target class hours
DECLARE id1_count BIGINT default 0; # t_stage_info stage 1 count
DECLARE id2_count BIGINT default 0; # t_stage_info stage 2 count
DECLARE id3_count BIGINT default 0; # t_stage_info stage 3 count
DECLARE firstEndTime datetime; # end time of the first stage
DECLARE secEndTime datetime; # end time of the second stage
DECLARE thirdEndTime datetime; # end time of stage 3
DECLARE done int default 0;

# DECLARE the cursor declare cur1 cursor for select p1.id, p1.start _ learn_time start_time, p1.last _ date end_time, p1.sum_study_time,p1.tar get_study_time FROM t_person_plan p1 left join t_person_plan_record p2 ON p1.id = p2.person _ plan_id WHERE p1.license _ type = 7 AND p1.last _ date <= CURRENT_DATE (); # t_person_plan_record p2: about more than 5 million data records # when the cursor reaches the end, mysql automatically sets done = 1 declare continue handler for SQLSTATE '000000' SET done = 1; OPEN cur1; REPEAT/* Move the cursor and assign a value */fetch cur1 into personPlaneId, startTime, endTime, sumStudyTime, targetStudyTime; if NOT done then # get the first stage end time SET firstEndTime = (SELECT add_time FROM t_person_plan_record WHERE person_plan_id = personPlaneId AND chapter_id = 29611 ); # obtain the second stage end time SET secEndTime = (SELECT add_time FROM t_person_plan_record WHERE person_plan_id = personPlaneId AND chapter_id = 29625 ); # get stage 3 end time SET thirdEndTime = (SELECT add_time FROM t_person_plan_record WHERE person_plan_id = personPlaneId AND chapter_id = 29734); # get the primary key id select count (1) into id1_count from t_stage_info where person_plan_id = personPlaneId AND stage = 1; SELECT count (1) into id2_count from t_stage_info where person_plan_id = personPlaneId AND stage = 2; SELECT count (1) into id3_count from region where person_plan_id = personPlaneId AND stage = 3; IF sumStudyTime <= 12 then if id1_count = 0 then insert into t_stage_info (person_plan_id, start_time, end_time, duration, stages) VALUES (personPlaneId, startTime, endTime, sumStudyTime, targetStudyTime, 1); else update t_stage_info SET end_time = endTime, period = sumStudyTime WHERE person_plan_id = personPlaneId AND stage = 1; end if; # ELSEIF sumStudyTime> 12 AND sumStudyTime <= 14 THEN # update the first-stage data IF id1_count = 0 then insert into t_stage_info (person_plan_id, start_time, end_time, timeout, target_study_time, stage) VALUES (personPlaneId, startTime, firstEndTime, sumStudyTime, targetStudyTime, 1); else update into SET end_time = firstEndTime, period = sumStudyTime WHERE person_plan_id = personPlaneId AND stage = 1; end if; # Update second-stage data IF id2_count = 0 then insert into t_stage_info (person_plan_id, start_time, end_time, hour, stage) VALUES (personPlaneId, firstEndTime, endTime, sumStudyTime, targetStudyTime, 2); else update t_stage_info SET end_time = endTime, sum_study_time = sumStudyTime WHERE person_plan_id = personPlaneId AND stage = 2; end if; # ELSEIF sumStudyTime> 14 then when learning the third stage # if thirdEndTime is null, SET thirdEndTime = case when thirdEndTime is null then endTime ELSE thirdEndTime END; # update the first-stage data IF id1_count = 0 then insert into t_stage_info (person_plan_id, start_time, end_time, latency, latency, stage) VALUES (personPlaneId, startTime, firstEndTime, sumStudyTime, targetStudyTime, 1); else update t_stage_info SET end_time = firstEndTime, sum_study_time = sumStudyTime WHERE person_plan_id = personPlaneId AND stage = 1; end if; # Update second-stage data IF id2_count = 0 then insert into t_stage_info (person_plan_id, start_time, end_time, hour, stage) VALUES (personPlaneId, firstEndTime, secEndTime, sumStudyTime, targetStudyTime, 2); else update t_stage_info SET end_time = secEndTime, sum_study_time = sumStudyTime WHERE person_plan_id = personPlaneId AND stage = 2; end if; # Update the third-stage data IF id3_count = 0 then insert into t_stage_info (person_plan_id, start_time, end_time, interval, stages) VALUES (personPlaneId, secEndTime, interval, sumStudyTime, targetStudyTime, 3); else update t_stage_info SET end_time = thirdEndTime, sum_study_time = sumStudyTime WHERE person_plan_id = personPlaneId AND stage = 3; end if; end if; UNTIL done end repeat; close cur1;

END
Now it takes two hours to get tens of thousands of data records,
I have created an index for 'person _ plan_id 'in the t_person_plan_record table,
In the t_stage_info table, indexes are created for person_plan_id. I don't know how to optimize other indexes,
How can I optimize it?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.