我有两张桌子tab1
和tab2
. tab1
有 39000 条记录和tab2
15000 条记录。tab1
并且tab2
都有两个共同的列说item_number
,color_code
。但是 in tab1
, color_code
可能是 null 但不是 in tab2
。item_number
在两个表中都不为空。在tab2
我必须插入一行(基于两列的唯一行)数据tab1
。为此,我编写了这样的程序。它将比较每一行,但时间太长。有什么方法可以提高性能。先取两个游标:
CURSOR CUR_tab1 IS SELECT item_number,color_code FROM
(SELECT item_number,color_code, SNO RID ,
MIN(SNO) OVER(partition BY item_number,color_code)
MIN_RID FROM tab1 ) WHERE RID= MIN_RID;
CURSOR CUR_tab2 IS SELECT sno_2,item_number,color_code,PRIORITY FROM tab2;
son
,是和sno_2
中的主键。tab1
tab2
要在一个表中查找记录,我使用的是for loop
.
SELECT NVL(MAX(SNO_2),0) INTO SNO_COUNT FROM tab2;
FOR CUR_tab1 IN CUR_NIIN_CAGE
LOOP
-- GET THE NIIN_CAGE_PART RECORDS DATA.
OLD_NNCGP_NIIN := CUR_tab1 .item_number;
OLD_NNCGP_HCC := CUR_tab1 .color_codeC;
IF (SNO_COUNT>0 AND OLD_NNCGP_HCC IS NOT NULL) THEN -- IF RECORDS EXISTS IN DB
FOR CUR_tab2 IN CUR_NIIN_HCC
LOOP
OLD_NIIN := CUR_tab2.item_number;
OLD_HCC := CUR_tab2.color_codeC;
NIIN_PRIORITY := CUR_tab2.PRIORITY;
IF (trim(OLD_NNCGP_NIIN) = trim(OLD_NIIN)
AND trim(OLD_NNCGP_HCC)=trim(OLD_HCC)) THEN
-- DO NOTHING
ROW_COUNT:=0;
PROCESSED :=FALSE;
EXIT;
ELSE
ROW_COUNT:=ROW_COUNT+1;
PROCESSED :=TRUE;
END IF;
END LOOP;
ELSIF (SNO_COUNT=0 AND OLD_NNCGP_HCC IS NOT NULL) THEN
PROCESSED :=TRUE;
ROW_COUNT:=ROW_COUNT+1;
END IF;
IF ( PROCESSED ) THEN
SNO_COUNT :=SNO_COUNT+1;
INSERT INTO tab2("SNO2","color_code","item_number","PRIORITY")
values (SNO_COUNT,OLD_NNCGP_HCC,OLD_NNCGP_NIIN,NULL);
COMMIT;
END IF;
PROCESSED:=FALSE;
ROW_COUNT:=0;
END LOOP;
但这需要将近 7 分钟。有没有更好的方法来比较列?