谁能告诉我以下哪一项更有效?我有数千万行要处理,性能至关重要。
在第二个示例中,table0
是一个临时表,它的创建似乎比第一个示例中的 table0 快得多。(为什么?)在第一个示例中我不能使用临时表,因为在创建表之前无法声明变量行。(table0
保存原始表的所有不同行,下面的代码中没有显示。)
我想在第二个示例中为 、 和 of 创建哈希索引是个好主意blah2
,尽管写入blah3
表需要更长的时间。blah4
blah5
table1
FOR row IN SELECT * FROM table0
LOOP
IF NOT EXISTS (SELECT 1 FROM table1
WHERE blah2 = row.blah2 AND blah3 = row.blah3
AND blah4 = row.blah4 AND blah5 = row.blah5) THEN
INSERT INTO table2
(blah0, blah1, blah2, blah3, blah4, blah5)
VALUES (row.blah0, row.blah1, row.blah2, row.blah3, row.blah4, row.blah5);
END IF;
END LOOP;
INSERT INTO table2
(blah0, blah1, blah2, blah3, blah4, blah5)
SELECT blah0, blah1, blah2, blah3, blah4, blah5 FROM table0
WHERE NOT EXISTS
(SELECT 1 FROM table1
WHERE table1.blah2 = table0.blah2
AND table1.blah3 = table0.blah3
AND table1.blah4 = table0.blah4
AND talbe1.blah5 = table0.blah5);