1. 我们建一个表 test2 举例说明 oracle 递归查询的两种方法。

数据结构如下:

2. 我们的目标是查找A1下面所有的子节点和所处层级。
A.通用做法:写一个递归SQL 如下
select t.*, level
from test2 t
start with t.father = 'A1'
connect by t.father = prior t.child
order by level
结果:
B.替代做法:
with all_test(father,
child,
levelno) as
(select t.father, t.child, 1
from test2 t
where t.father = 'A1'
union all
select t2.father, t2.child, at.levelno + 1
from test2 t2, all_test at
where at.child = t2.father)
select * from all_test
结果:
3. 从以上两种结果来看,结果一样。经测试数据量很大的情况下,B,替代做法效率较高。
本文介绍了Oracle数据库中进行递归查询的两种方法:通用的START WITH CONNECT BY语句和替代做法。通过举例说明如何查询特定节点的所有子节点及其层级,并比较了在大量数据时两种方法的效率,指出在大数据量下,替代做法表现更优。
3718

被折叠的 条评论
为什么被折叠?



