且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Oracle中使用大量数据在同一张表中进行字段比较的最快方法

更新时间:2023-08-28 15:04:52

首先,我相信您的任务可以使用稳定的SQL实现(并且应该实际上是).没有花哨的光标,没有循环,只需选择,插入和更新.我将从取消数据源入手开始(目前尚不清楚您是否有主键来连接两组数据集):

First of all I believe that your task can be implemented (and should be actually) with staight SQL. No fancy cursors, no loops, just selects, inserts and updates. I would start with unpivotting your source data (it is not clear if you have primary key to join two sets, I guess you do):

Col0_PK    Col1    Col2    Col3    Col4
----------------------------------------
Row1_val   A       B       C       D
Row2_val   E       F       G       H

以上是您的源数据.使用 UNPIVOT子句,我们将其转换为:

Above is your source data. Using UNPIVOT clause we convert it to:

Col0_PK     Col_Name    Col_Value
------------------------------
Row1_val    Col1        A
Row1_val    Col2        B
Row1_val    Col3        C
Row1_val    Col4        D
Row2_val    Col1        E
Row2_val    Col2        F
Row2_val    Col3        G
Row2_val    Col4        H

我想你明白了.假设我们的table1具有一组数据,而相同的结构化table2具有第二组数据.***使用索引组织的表.

I think you get the idea. Say we have table1 with one set of data and the same structured table2 with the second set of data. It is good idea to use index-organized tables.

下一步是将行相互比较并存储差异明细.像这样:

Next step is comparing rows to each other and storing difference details. Something like:

insert into diff_details(some_service_info_columns_here)
 select some_service_info_columns_here_along_with_data_difference
  from table1 t1 inner join table2 t2
     on t1.Col0_PK = t2.Col0_PK
    and t1.Col_name = t2.Col_name
    and nvl(t1.Col_value, 'Dummy1') <> nvl(t2.Col_value, 'Dummy2');

在最后一步,我们更新差异汇总表:

And on the last step we update difference summary table:

insert into diff_summary(summary_columns_here)
 select diff_row_id, count(*) as diff_count
  from diff_details
 group by diff_row_id;

这只是显示我的方法的粗略草稿,我相信应该考虑更多细节.总而言之,我建议两件事:

It's just rough draft to show my approach, I'm sure there is much more details should be taken into account. To summarize I suggest two things:

  1. UNPIVOT数据
  2. 使用SQL语句代替游标
  1. UNPIVOT data
  2. Use SQL statements instead of cursors