2013-03-04 180 views
1

以下查詢似乎需要大約20秒才能執行,並且因爲它在單個 事務中多次運行,它會嚴重影響性能。SqL查詢性能問題

[update table1 
      set column3 = 'new_str' 
      where column1||','||column2 in 
     (select table1.column1||','||column2 
      from table1 
     join table2 on table1.column1 = table2.column1 
     where table2.column4 = 'value4' 
     and table1.column2 = 'value2' 
     and column3 = 'old_str')] 

表1
列1 - 炭(12) - 主鍵
列2 - 炭(30) - 主鍵
欄3 - 炭(25)

table2中
COLUMN1 - 炭( 12) - 主鍵(表1中的外鍵)
column4 - char(12)

上表包含約1009578和1082555記錄。

+2

什麼味道的SQL? – dnagirl 2013-03-04 14:51:23

+0

有了這麼多的記錄,我會考慮節省更多的時間,而不是查詢優化。檢查可以更新,刪除或優化的索引,並利用一些基本標準的視圖來消除您正在梳理的一些記錄。也許上個月的記錄或者X狀態的記錄都是這些行的記錄。你在用什麼數據庫? – 2013-03-04 16:07:13

回答

0

無法測試它,但在我看來,打破基於計算字段的標準應該會加快更新速度。東西(可能是缺少的東西)這樣應該會更好:

[update table1 
     set column3 = 'new_str' 
      where column1 in 
     (select table1.column1 
      from table1 
     where table1.column2 = 'value2' 
     and column3 = 'old_str') 
     and 
     column2 in 
    (select table2.column2 
      from table2 
     where table2.column1 = column1 
     and table2.column4 = 'value4') 
     ] 
0

我認爲你正在對Table1做不必要的查詢。試試這個:

update table1 t1 
set column3 = 'new_str' 
where EXISTS 
    (select *   
    from table2 t2 
    where 
    t1.column1 = t2.column1 -- this is your link from t1 to t2 
    and t2.column4 = 'value4' 
    and t1.column2 = 'value2' 
    and t2.column3 = 'old_str' 
    ) 
0

我想這IN原因不是necesary這裏:

update table1 
    set column3 = 'new_str' 
    from table1 join table2 on table1.column1 = table2.column1 
     where table2.column4 = 'value4' 
     and table1.column2 = 'value2' 
     and table1.column3 = 'old_str' 

寫我們的解決方案是最快的;]!

+0

非常感謝您的建議,這個解決方案似乎是我用例中最快的。 – user338804 2013-03-04 17:50:10

+0

所以請考慮將此問題標記爲已回答。謝謝! – www 2013-03-04 19:12:40