简体   繁体   中英

why there is a patteren of execution time in mySql procedure execution

I have a stored procedure to generate some output in mySql.

BEGIN
  START TRANSACTION;

  CREATE TEMPORARY TABLE IF NOT EXISTS tempDays
  (
    ID int AUTO_INCREMENT PRIMARY KEY,
    -- column list

  );
 CREATE TEMPORARY TABLE IF NOT EXISTS tempPeriods
  (
    ID int AUTO_INCREMENT PRIMARY KEY,
    -- column list

  );
  INSERT INTO tempDays (-- column list) SELECT -- select list contains n number of rows with some conditions
--
--
-- some queries and temporary table creation
--
--
--
  SELECT COUNT(*) INTO @daylimit FROM tempDays;
  SELECT 1 INTO @daycounter;

  WHILE(@daycounter<=@daylimit)
  DO

       SELECT COUNT(*) INTO @limitperiods FROM tempPeriods;
    SELECT 1 INTO @countperiods;

      WHILE(@countperiods<=@limitperiods)
      DO
         -- some complicated(some sort of IF THEN blocks to take time some execution time) query inside.
          SET @countperiods=@countperiods+1;
      END WHILE;
  SET @daycounter=@daycounter+1;
  END WHILE;

  SELECT * FROM temptimetable;
  TRUNCATE TABLE tempDays;
  TRUNCATE TABLE tempPeriods;
 END TRANSACTION;
END 

This is the execution summary of 8 one by one calls.

Stored procedure 'sample_db.sample_proc' has been executed in 1.565s.
Stored procedure 'sample_db.sample_proc' has been executed in 2.362s.
Stored procedure 'sample_db.sample_proc' has been executed in 1.541s.
Stored procedure 'sample_db.sample_proc' has been executed in 2.198s.
Stored procedure 'sample_db.sample_proc' has been executed in 1.863s.
Stored procedure 'sample_db.sample_proc' has been executed in 3.657s.
Stored procedure 'sample_db.sample_proc' has been executed in 2.306s.
Stored procedure 'sample_db.sample_proc' has been executed in 3.226s.

we can see that an execution time pattern... 1,2,1,2,3,2,3

Can you please explain is there anything related to the execution time depends on previous execution. One I found is that, there is no need to re-create the temporary table will cause decrease execution time. But if execute it again we can see execution time increasing. How? this is the execution graph..

Time is not the best performance measure. This might occur due to various factors not related directly to your stored procedure: IO, memory grant, etc. Especially considering such relatively short run times.

You would have to be able to see guts of mysql io ops, execution plan for query, etc to be able to determine what is exactly happening.

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