[英]MySQL Stored Procedure for Complex Query takes 5 minutes
此过程正在运行,但是需要5分钟才能运行。 有没有更快,更简单的方法?
我有一张表,其中包含检查清单。 以后在同一表中插入了后续检查。 后续检查具有相同的序列号,但具有不同的date_stamp。 我需要找出哪些检查尚未完成跟进检查。
所以首先我要获取所有type_due = 3且未标记为inspection_completed的序列号。
然后,从该序列号列表中,我正在检查是否在以后的date_stamp上进行了具有相同序列号的检查,该检查被标记为inspection_completed。
因此,只有在存在其他条件时才是选择。 如果第二个查询中的结果具有相同的serial_number,更大的日期和Inspection_completed值不为null,则消除第一个查询中的结果池。
每次运行此查询时,我都会截断Inspections_due表,因为添加到Inspections表中的新的后续检查将使Inspections_due表中的某些结果不再有效。
第一个查询大约有9,000个结果。 总体而言,编写另一个查询以删除Inspections_due表中的无效结果,并且每次运行此查询时仅将新记录添加到Inspections_due可能总体上更快。
注意:我尝试使用Select count(*)代替Select Exists,但是速度差异不大。
任何建议都很好! 谢谢
CREATE PROCEDURE create_due_inspections()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE iid int(10); #unique inspection id
DECLARE sn VARCHAR(16); #serial number
DECLARE dt date; #date_stamp
DECLARE `result` int;
DECLARE cur1 CURSOR FOR SELECT inspection_id, serial_number, date_stamp FROM
Inspections where type_due = '3' and inspection_completed is null;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
TRUNCATE TABLE `Inspections_due`; # clean out the old results
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO iid,sn,dt;
IF done THEN
LEAVE read_loop;
END IF;
SET `result` := (select exists(Select inspection_id from Inspections
where serial_number = sn and date_stamp > dt and inspection_completed is not null));
IF `result` THEN
insert into Inspections_due values(iid);
END IF;
END LOOP;
CLOSE cur1;
END;
您应该避免使用游标并使用set :
INSERT into Inspections_due
SELECT
inspection_id as iid
FROM
Inspections I1
where
type_due = '3' and inspection_completed is null
and exists(
Select inspection_id
from Inspections I2
where I2.serial_number = I1.serial_number
and I2.date_stamp > I1.date_stamp
and inspection_completed is not null))
还可以通过内部联接替换现有子查询 :
SELECT distinct
inspection_id as iid
FROM
Inspections I1
inner join
Inspections I2
on
I2.serial_number = I1.serial_number
and I2.date_stamp > I1.date_stamp
and inspection_completed is not null
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.