2014-04-07 23 views
0

有人可以請幫我在優化以下查詢......如何使用表連接優化此查詢?

UPDATE tab1 SET col1 = 'NA' where 
col2 =(Select col1 from tab2 where col2 is null and 
col3 = (Select col1 from tab3 where col2 = 100)); 
+0

請分享樣本數據,我知道這是不是完全必需的,但可以加快我答。 – AK47

+0

需要注意的是,根據記錄的數量,您可以通過確保參考列進行索引來進一步優化查詢。所有這些都取決於您的數據結構和數據量。 – llanato

+0

您可能會發現語法差異,具體取決於運行哪個數據庫。 –

回答

0
update t1 
SET col1 = 'NA' 
from tab1 t1 
inner join tab2 t2 on t1.col2=t2.col1 and t2.col2 is null 
inner join tab3 t3 on t2.col3=t3.col1 and t3.col2 = 100 
+0

我可能是錯的,但爲了得到與原始查詢相同的結果,不應該將tab3連接到tab2而不是tab1? – vishad

+0

謝謝布迪...... – Luv