【发布时间】:2012-01-06 23:16:36
【问题描述】:
我们有一个“合并”脚本,用于将代码分配给客户。目前,它通过在临时表中查看客户并为其分配未使用的代码来工作。这些代码被标记为已使用,并且带有代码的分段记录加载到生产表中。暂存台被清理干净,生活变得美好。
不幸的是,我们现在正在处理更大的数据集(包括客户和代码),并且该过程需要很长时间才能运行。我希望这里的优秀社区可以查看这里的代码并提供改进或解决问题的另一种方式。
提前致谢!
编辑 - 忘记提及其中一些检查的部分原因是临时表是“活动的”,并且可以在脚本运行期间将记录输入其中。
whenever sqlerror exit 1
-- stagingTable: TAB_000000003134
-- codeTable: TAB_000000003135
-- masterTable: TAB_000000003133
-- dedupe staging table
delete from TAB_000000003134 a
where ROWID > (
select min(rowid)
from TAB_000000003134 b
where a.cust_id = b.cust_id
);
commit;
delete from TAB_000000003134
where cust_id is null;
commit;
-- set row num on staging table
update TAB_000000003134
set row_num = rownum;
commit;
-- reset row nums on code table
update TAB_000000003135
set row_num = NULL;
commit;
-- assign row nums to codes
update TAB_000000003135
set row_num = rownum
where dateassigned is null
and active = 1;
commit;
-- attach codes to staging table
update TAB_000000003134 d
set (CODE1, CODE2) =
(
select CODE1, CODE2
from TAB_000000003135 c
where d.row_num = c.row_num
);
commit;
-- mark used codes compared to template
update TAB_000000003135 c
set dateassigned = sysdate, assignedto = (select cust_id from TAB_000000003134 d where c.CODE1 = d.CODE1)
where exists (select 'x' from TAB_000000003134 d where c.CODE1 = d.CODE1);
commit;
-- clear and copy data to master
truncate table TAB_000000003133;
insert into TAB_000000003133 (
<custmomer fields>, code1, code2, TIMESTAMP_
)
select <custmomer fields>, CODE1, CODE2,SYSDATE
from TAB_000000003134;
commit;
-- remove any staging records with code numbers
delete from TAB_000000003134
where CODE1 is not NULL;
commit;
quit
【问题讨论】:
-
在原帖中添加了一些额外的细节