2017-03-23 40 views
-2

我的過程查詢需要19.546s,您可以在下面的快照底部看到。你可以看到總數據是60370項。程序查詢花費很多時間,19.546s很長。如何優化程序方法?

enter image description here

那麼,有沒有優化我的程序的方法?這是我的程序:

BEGIN 
    declare icount int default 0; 
    declare exit handler for SQLEXCEPTION set out_a=1; 
    select count(*) into icount from P01 where P0101=in_a; 
    if icount=0 THEN 
    set out_a=1; 
    set out_b=0; 
    set out_c=''; 
    set out_d=''; 
    ELSE 
    set out_a=0; 
    select P0106,P0107,P0108 into out_b,out_c,out_d from P01 where P0101=in_a; 

    if in_b=1 then 
     select P0202,P0203,P0204,P0205 from P02 where P0201=in_a and P0204=out_b+1 order by P0202; 
    ELSE 
     select P0202,P0203,P0204,P0205 from P02 where P0201=in_a and P0204>=out_b+1 order by P0202; 
    end if; 
    end if; 

END 

有沒有辦法縮短程序花費的時間?


編輯

如果我詢問我的表p02,將斥資20 + S:

select * from p02; 

enter image description here

所以,這裏也許這個問題?

+0

對這些查詢運行'EXPLAIN',找到您缺少索引的位置。 – miken32

+0

@ miken32我更新了我的問題,可以看出,查詢表格還需要很長時間。 –

回答

0
  1. 上表P02創建索引:

    create index i_P0201 on P02(P0201); 
    create index i_P0202 on P02(P0202); 
    create index i_P0204 on P02(P0204); 
    
    create index i_P0101 on P01(P0101); 
    
  2. 避免在查詢算術運算:P0204>=out_b+1,只是用計算出的值,如:

    set @out_b_plus1 = out_b+1; 
    

    而且在查詢中使用該值:

    `P0204 >= @out_b_plus1` 
    

也許優化會少幾秒鐘,因爲您的表格有大量數據。

+0

但'select * from p02'也是20+ s。什麼問題導致了很長時間? –

+0

20年代是由數據量轉移到客戶端 –

+0

,但60K項目數據將花費20 + s轉移到客戶端? –