欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

mysql-请教存储过程执行很慢,如何优化?

程序员文章站 2022-04-25 18:58:25
...
mysql优化存储过程

BEGIN
DECLARE personPlaneId BIGINT;

DECLARE starttime datetime; #开始时间
DECLARE endtime datetime; #结束时间
DECLARE sumStudyTime INT; #当前学时
DECLARE targetStudyTime INT; #目标学时
DECLARE id1_count BIGINT default 0; #t_stage_info 第一阶段count
DECLARE id2_count BIGINT default 0; #t_stage_info 第二阶段count
DECLARE id3_count BIGINT default 0; #t_stage_info 第三阶段count
DECLARE firstEndTime datetime; #第一阶段结束时间
DECLARE secEndTime datetime; #第二阶段结束时间
DECLARE thirdEndTime datetime; #第三阶段结束时间
DECLARE done INT DEFAULT 0;

    #声明光标        DECLARE cur1 CURSOR FOR            SELECT p1.id,            p1.start_learn_time start_time,            p1.last_date end_time,            p1.sum_study_time,p1.target_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 12 AND sumStudyTime14 THEN                        #如果thirdEndTime为空则取最后学习的时间                        SET thirdEndTime = CASE WHEN thirdEndTime IS NULL THEN endTime ELSE thirdEndTime END;                        #更新第一阶段数据                        IF id1_count=0 THEN                            INSERT INTO t_stage_info(person_plan_id,start_time,end_time,sum_study_time,target_study_time,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;                        #更新第二阶段数据                        IF id2_count=0 THEN                            INSERT INTO t_stage_info(person_plan_id,start_time,end_time,sum_study_time,target_study_time,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;                        #更新第三阶段数据                        IF id3_count=0 THEN                            INSERT INTO t_stage_info(person_plan_id,start_time,end_time,sum_study_time,target_study_time,stage)                                 VALUES(personPlaneId,secEndTime,thirdEndTime,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;    end if;        UNTIL done END REPEAT;      close cur1; 

END
现在执行了2小时才有了10几万数据,
我在t_person_plan_record 表里对‘person_plan_id ’建了索引,
在表t_stage_info 对person_plan_id ’建立了索引,其他我就不知道怎么优化了,
请问大神,还能怎么优化比较好呢?