Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8734303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:49:02+00:00 2026-06-13T09:49:02+00:00

This procedure is working but it takes 5 minutes to run. Is there a

  • 0

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;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:49:03+00:00Added an answer on June 13, 2026 at 9:49 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to call this procedure that sends one value that can be NULL
I have this procedure that executes another procedure passed by a parameter and its
I have been working on this script for a long time, but the code
I have a stored procedure that takes two parameters as varchar(50) . The stored
I have a rather large stored procedure that I'm working with, so I'll only
I have this procedure i my package: PROCEDURE pr_export_blob( p_name IN VARCHAR2, p_blob IN
I have a paintbox and I draw a TBitmap like this: procedure MyForm.PaintBoxPaint(Sender: TObject);
My website has a search procedure that runs very slowly. One thing that slows
I have stored procedure that insanely times out every single time it's called from
I have a .Net COM server that has a method that takes a message

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.