[英]Performance of insert statement with CTE
我有 2 個外部表,我們稱它們為 A 和 B。我還有一個內部表 C。A 和 B 有一些重疊的值。 我想編寫一個程序,將 A 中的所有值寫入 C,然后將 B 中所有我還沒有(從 A)寫入 C 的值。所以首先將 A 寫入 C,然后寫入所有尚未寫入的剩余數據從 B 寫到 C。基本上是這樣的:
Write A -> C
Write B where not already written by A -> C
我的第一次嘗試看起來像這樣
insert into C (select * from A);
insert into C (select * from B where b.id not in (select a.id from a));
我的表很大,數據鏈接很慢,所以這不會發生,因為它需要很長時間。 我認為問題在於它必須為 B 的每個條目執行(select a.id from a)
。所以我認為使用 CTE 肯定會有所幫助:
insert into C (select * from A);
insert into C
with A_values as (select a.id from a)
select * from b where b.id not in a.values;
但是,這仍然不會終止(花費太長時間)。 有什么想法可以解決這個問題嗎?
我想你在描述兩個插入:
insert into c ( . . . )
select . . .
from a;
insert into c ( . . . )
select . . .
from b
where not exists (select 1 from c where b.id = c.id);
為了提高性能,您需要在c(id)
或任何用於比較的列上建立索引。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.