【问题标题】:START WITH using exists condition in Oracle sql在 Oracle sql 中使用存在条件开始
【发布时间】:2017-09-24 02:13:53
【问题描述】:

我正在尝试使用 start with 子句在 Oracle 中获取分层数据。我有以下两个表:

TableA(模式)(600 万行): 身份证,费用

TableB(模式)(350 亿行): id, parent_id

我们想从表 A 中递归地获取所有与 id 关联的 parent_id。类似于下面的查询:

Select * from TableB START WITH id in ( select id from TableA ) 
connect by id = prior parent_id and parent_id != id

虽然上述方法适用于 TableA 的少量行,但我正在尝试在 TableA 有 600 万行而 TableB 有 350 亿行的情况下执行此操作。

在这种情况下获取分层数据的推荐方法是什么(例如存在子句)?尝试 'between' 也会产生很多不需要的 id ..

非常感谢任何帮助。

【问题讨论】:

  • TableA 的条件是什么?!

标签: sql oracle


【解决方案1】:

有了这个大数据集,你需要很好地过滤你从 TableA 中选择的内容

您的查询是正确的,但需要 TableA 上的条件

select * from TableB
start with id in ( select id from TableA where <condition on TableA>) 
connect by id = prior parent_id and parent_id != id

最重要的是: - TableA 在您要用于过滤的列上具有索引(条件) - TableB 在 ID 上有主键或唯一键

如果您的查询最终使用 TableA 上的索引(并检索选择性不是大量的列),并在 TableB 上使用 UNIQUE 键,那么它应该表现良好。

但是,我计算出 A 中每条记录在 B 中的平均记录约为 6,000 条记录!

如果您需要减少TableB检索到的记录集,那么您最好在connect by之前这样做

 select * 
 from (select * TableB where <condition to be applied before connect by>)
 start with id in (select id from TableA where <condition on TableA>)
 connect by id = prior parent_id and parent_id != id

实际上,如此大量的数据应该始终正确分区,您没有提及

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    相关资源
    最近更新 更多