【发布时间】:2020-12-09 18:27:39
【问题描述】:
我有三张桌子。 T1 是主节点,存储唯一 ID。 我想获取 t2 和 t3 中不存在但在 t1 中存在的 ID 我写了一个查询。不确定它是否正确并优化了一个。需要帮助以比这更好的方式编写查询,因为所有表中都有大量数据。
Select t1.ID
from t1
where ID not in (
Select distinct t2.ID from t2
Union
Select distinct t3.ID from t3
)
and col2 ='A'
【问题讨论】:
-
更多的是关于执行计划,它取决于您的数据分布和所有 3 个表的基数。通常在所有大表的情况下,最好得到2个
hash join anti,所以通常not exists、not in和left join x where x.id is null比设置运算符好(设置加入转换默认禁用,默认启用仅适用于最新的 oracle 版本) -
向我们展示以下 3 个查询的结果:1.
Select count(*), count(distinct ID) from t1 where col2 ='A'2)select count(*), count(distinct ID) from t23)select count(*), count(distinct ID) from t3
标签: sql oracle oracle11g subquery query-optimization