我有太多的 SELECT 语句,只有一个 INSERT(可能有数百个)而且系统性能很差。
我将用一般语言解释正在发生的事情以及我正在寻找的内容:
考虑 Oracle PL/SQL 中的以下两个伪代码,它们中的哪一个会提供最佳性能?
选项 A:
INSERT INTO MyTable
WITH Fields AS (
SELECT Field1, Field2, ..., FieldN FROM TableA JOIN TableW .... WHERE <condition1>
UNION ALL
SELECT Field1, Field2, ..., FieldN FROM TableB JOIN TableX .... WHERE <condition2>
UNION ALL
SELECT Field1, Field2, ..., FieldN FROM TableC JOIN TableB .... WHERE <condition3>
....
UNION ALL
....
SELECT Field1, Field2, ..., FieldN FROM TableZZZ JOIN TableB .... WHERE <conditionN>
选项 B:
BEGIN
INSERT INTO MyTable SELECT Field1, Field2, ..., FieldN FROM TableA JOIN TableZ .... WHERE <condition1>
INSERT INTO MyTable SELECT Field1, Field2, ..., FieldN FROM TableB JOIN TableW .... WHERE <condition2>
INSERT INTO MyTable SELECT Field1, Field2, ..., FieldN FROM TableC JOIN TableH .... WHERE <condition3>
...
INSERT INTO MyTable SELECT Field1, Field2, ..., FieldN FROM TableZZZZ JOIN TableX .... WHERE <conditionN>
END
我没有放真实的表名,但我想知道:如果我将当前选项 A 更改为选项 B,它会为我提供更好的性能吗?我的意思是,在这种情况下用许多 INSERT 语句替换 UNION ALL 是个好主意吗?