首页 > 解决方案 > oracle 中的合并命令导致空间不足问题

问题描述

我在 oracle db 中有 300 万条记录的 table1。我需要使用 table2 和 table3 中的数据更新 table1 中大约 200 万条记录的一列。

我对这 3 个表的数据模型是

table1 has id, DOI
table2 has product_code, id
table3 has DOI and productcode

我正在使用合并命令,因为在这种情况下更新命令非常耗时。

merge into table1
using 
(select p.id, stg.doi from table2 p, table3 stg, table1 pc where stg.productcode = p.product_code and pc.product_id = p.id) st 
on (st.id = table1.product_id)
when matched then update set table1.doi = st.doi;
commit;

由于所有这 3 个表都有数百万条记录,临时表总是空间不足导致合并脚本突然停止。DBA 已分配 64gb 的空间,但我们的临时空间仍然不足。

有什么建议么?

标签: sqloracle12c

解决方案


您可以添加WHERE子句:

merge into product_content
using (select p.id, stg.doi
       from table2 p
       JOIN table3 stg ON stg.productcode = p.product_code
       JOIN table1 pc  ON pc.product_id = p.id) st 
       -- modern explicit JOIN
on (st.id = product_content.product_id)
when matched then update set product_content.doi = st.doi
                  where product_content.doi != st.doi;  
                  -- assuming that doi is defined as NOT NULL
                  -- there is no need for update if nothing has changed

推荐阅读