我需要同步两个表.我们假设这些表包含以下列:
Table1: A, B, C, D
Table2: A, B, C, E
Run Code Online (Sandbox Code Playgroud)
我需要在Table1中找到这样的行,表2中没有相应(A, B, C)值的条目,然后将E计算为F(D)并更新Table2.
如果我需要匹配例如只有A,我会写下面的查询:
SELECT * FROM Table1 WHERE A NOT IN (SELECT A FROM Table2)
Run Code Online (Sandbox Code Playgroud)
多列模拟似乎太慢了:
SELECT * FROM Table1 WHERE A NOT IN (SELECT A FROM Table2)
AND B NOT IN (SELECT B FROM Table2)
AND C NOT IN (SELECT C FROM Table2)
Run Code Online (Sandbox Code Playgroud)
编写此类查询的最佳方法是什么?
Ron*_*nis 20
如果两个表中的(a,b,c)都不是NULL,那么NOT IN和NOT EXISTS很可能(在我尝试过的verisons上)生成相同的执行计划.
如果(a,b,c)被声明为可为空,但是你知道这些列实际上不是null,你可以通过添加"AND a is not null AND b not not null来欺骗优化器进行散列反连接对于您的查询,AND c不为空.(您可能还需要在子查询中添加/*+ HASH_AJ*/提示.)
此外,以下查询不相同:
from table1
where (a,b,c) not in (select a,b,c from table2)
from table1
where a not in(select a from table2)
and b not in(select b from table2)
and c not in(select c from table2)
Run Code Online (Sandbox Code Playgroud)
SELECT * FROM Table1
WHERE (A, B, C) NOT IN
(SELECT A,B,C FROM Table2)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
39345 次 |
| 最近记录: |