简体   繁体   中英

MySQL Stored Procedure for Complex Query takes 5 minutes

This procedure is working but it takes 5 minutes to run. Is there a faster, simpler way?

I have one table that contains a list of inspections. There are follow up inspections inserted at a later date into the same table. The follow up inspections have the same serial_number but a different date_stamp. I need to find out which inspections do not have completed follow up inspections.

So first I'm getting all the serial_numbers that are of type_due=3 and are not marked as inspection_completed.

Then, from this list of serial_numbers, I am checking if there is an inspection with the same serial_number at a later date_stamp that is marked as inspection_completed.

So it is a select only if something else exists; an elimination of the pool of results in the first query if there is a result in the second query with the same serial_number, greater date, and an inspection_completed value that is not null.

I am truncating the Inspections_due table each time this query is run because new follow up inspections that are added to the Inspections table will make some results in the Inspections_due table no longer valid.

There are about 9,000 results from the first query. Maybe it would be faster overall to write another query to remove invalid results in the Inspections_due table and only add the new records to Inspections_due each time this query is run.

Note: I tried Select count(*) instead of Select Exists, but not much speed difference.

Any suggestions would be great! Thanks

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;

You should avoid cursor and work with sets :

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))

Also replace exists subquery by a inner join :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM