2017-01-26 2 views
-1

mySqlに出力を生成するストアドプロシージャがあります。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<[email protected]) 
    DO 

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

     WHILE(@countperiods<[email protected]) 
     DO 
     -- some complicated(some sort of IF THEN blocks to take time some execution time) query inside. 
      SET @[email protected]+1; 
     END WHILE; 
    SET @[email protected]+1; 
    END WHILE; 

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

これは、1つずつ呼び出しの実行概要です。私たちは... 1,2,1,2,3,2,3

あなたが説明していただけますが、実行時間に関連するものがあり、実行時間パターンことがわかります

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. 

は以前に依存します実行。私が見つけたのは、一時テーブルを再作成する必要がないため、実行時間が短縮されることです。しかし、再度実行すると、実行時間が増えることがわかります。どうやって? this is the execution graph..

答えて

0

時間は最高のパフォーマンス尺度ではありません。これは、ストアドプロシージャに直接関係しないさまざまな要因(IO、メモリグラントなど)が原因で発生する可能性があります。

正確に何が起きているのかを判断できるようにするには、mysql io ops、クエリの実行計画などを確認する必要があります。

+0

最高のパフォーマンス指標が何であるか教えてください。 –

+0

私の場合は、実行時間を最小限に抑える必要があります。 –

+0

理想的には、データベースの読み取り、メモリ要件の組み合わせを使用して、クエリ実行の各ステップで返される行の数を確認します。あなたのケースでは、WHILEループを助け、機能をクエリとして書き直すように見えます。ループを使用することは、あらゆる種類のSQL実装に可能な最悪の事の1つです –

関連する問題