-2

我有以下查询,想知道是否有更好的方法来调整它?

MERGE INTO target_table TARGET USING
(SELECT DISTINCT g1.column_name
FROM test_gtt1 g1,
test_gtt2 g2
WHERE g1.column_name = g2.column_name
AND g1.diff_id      IS NULL
AND g2.diff_id      IS NULL
UNION ALL
SELECT DISTINCT g1.column_name
FROM test_gtt1 g1,
test_gtt2 g2
WHERE 
g1.column_name  = g2.column_name
AND g1.diff_id = g2.diff_id
AND g1.diff_id IS NOT NULL
AND g2.diff_id IS NOT NULL
) SOURCE ON ( TARGET.column_name = SOURCE.column_name)
WHEN MATCHED THEN
UPDATE SET TARGET.column_name2 = 123;
4

2 回答 2

1

这取决于数据类型、索引和生成的计划是什么(如果您提供了它会很有帮助)。

但是联合显然是多余的:

SELECT DISTINCT g1.column_name
FROM test_gtt1 g1,
test_gtt2 g2
WHERE g1.column_name = g2.column_name
AND ((g1.diff_id IS NULL
      AND g2.diff_id IS NULL)
     OR (g1.diff_id IS NOT NULL
         AND g2.diff_id IS NOT NULL))

更新

我突然想到(在回复 APC 的评论时)也可以使用...获得相同的结果。

SELECT DISTINCT g1.column_name
FROM test_gtt1 g1,
test_gtt2 g2
WHERE g1.column_name = g2.column_name
AND DECODE(g1.diff_id, NULL, -1, 1)=DECODE(g2.diff_id, NULL, -1, 1);
于 2013-03-14T11:53:09.027 回答
-1

如果您要将这两组与 UNION 结合使用diff_id is null,则无需测试:diff_id is not null

       select * from foo where bar is null
       union all
       select * from foo where bar is not null

是相同的

       select * from foo
于 2013-03-14T12:03:02.703 回答