我想在 MERGE 语句中更新多个列,但是对于每一列,条件都不同。我该如何实现。
我在两个表中都有超过 100 万行。所有列都是数字。除了 Id,所有 3 列的数字都精度约为 18 位,例如:1.34255353433230675
有没有更好的更新方法。每天可能会更新大约 50,000 行所以我必须将更新值合并到目标表。我尝试了 UPDATE 和 FORALL,但速度很慢。
我基本上想根据公共 ID 列合并差异。还有其他方法更好吗?
DECLARE
TYPE test1_t IS TABLE OF test.score%TYPE INDEX BY PLS_INTEGER;
TYPE test2_t IS TABLE OF test.id%TYPE INDEX BY PLS_INTEGER;
TYPE test3_t IS TABLE OF test.Crank%TYPE INDEX BY PLS_INTEGER;
TYPE test4_t IS TABLE OF test.urank%TYPE INDEX BY PLS_INTEGER;
vscore test1_t;
vid test2_t;
vcrank test3_t;
vurank test4_t;
BEGIN
SELECT id,score,crank,urank
BULK COLLECT INTO vid,vscore,vcrank,vurank
FROM test;
FORALL i IN 1 .. vid.COUNT
MERGE INTO final T
USING (SELECT vid (i) AS o_id,
vcrank (i) AS o_crank,
vurank (i) AS o_urank
vscore (i) AS o_score
FROM DUAL) S
ON (S.o_id = T.id)
WHEN MATCHED THEN
UPDATE SET T.crank = S.o_crank
WHERE T.crank <> S.o_crank;
UPDATE SET T.crank = S.o_crank
WHERE T.crank <> S.o_crank;
UPDATE SET T.crank = S.o_crank
WHERE T.crank <> S.o_crank;
UPDATE SET T.score = S.score
WHERE T.score <> S.score;
-- I tried the below case its not working either...
-- UPDATE SET T.crank = (CASE WHEN T.crank <> S.o_crank
-- THEN S.o_crank
-- END),
-- T.urank = (CASE WHEN T.urank <> S.o_urank
-- THEN S.o_urank
-- END);
COMMIT;
END;
/