【发布时间】:2021-02-23 16:45:17
【问题描述】:
我有一个包含列的表格
parent_key1, parent_key2, child_key1, child_key2
通过两对参数的连接来定义树。
表相当大,包含数千个根对象,也就是小时候不会出现的父对象;可以这么说,这张桌子不包含一棵树,而是一片森林。这就是查询 here 不起作用的原因。
我想获取以 top_ancestor_key1 和 top_ancestor_key2 开头的树成员。
对于一个程序,我可以定义两个参数:top_ancestor_key1和:top_ancestor_key2,代码
SELECT parent_key1, parent_key2, child_key1, child_key2, level
FROM genealogy
START WITH parent_key1 = :top_ancestor_key1, parent_key2 = :top_ancestor_key2,
CONNECT BY parent_key1 = PRIOR child_key1 AND parent_key2 = PRIOR child_key2
效果很好。
现在我想用列创建一个视图 «ancestors_resolved»
top_ancestor_key1, top_ancestor_key2, parent_key1, parent_key2, child_key1, child_key2 [, level]
我可以将结果用于连接 top_ancestor_key1 和 top_ancestor_key2
我试过了
--CREATE View ancestors_resolved AS
SELECT connect_by_root parent_key1 as top_ancestor_key1, connect_by_root parent_key2 as top_ancestor_key2, parent_key1, parent_key2, child_key1, child_key2, level
FROM genealogy
CONNECT BY parent_key1 = PRIOR child_key1 AND parent_key2 = PRIOR child_key2
然而,封闭的查询
SELECT * FROM
(
SELECT connect_by_root parent_key1 as top_ancestor_key1, connect_by_root parent_key2 as top_ancestor_key2, parent_key1, parent_key2, child_key1, child_key2, level
FROM genealogy
CONNECT BY parent_key1 = PRIOR child_key1 AND parent_key2 = PRIOR child_key2
)
WHERE top_ancestor_key1='grandpa' AND top_ancestor_key2 = 5
超时;似乎 oracle 试图在评估参数之前构建所有树。
我也试过
WITH tmptbl (parent_key1, parent_key2, child_key1, child_key2) as (
SELECT parent_key1, parent_key2, child_key1, child_key2
FROM genealogy
UNION ALL
SELECT tmptbl.parent_key1, tmptbl.parent_key2, tmptbl.child_key1, tmptbl.child_key2
FROM tmptbl
INNER JOIN genealogy x on x.child_key1 = tmptbl.parent_key1 and x.child_key2 = tmptbl.parent_key2 and x.child_key1 != x.parent_key1 and x.child_key2 != x.parent_key2
)
SELECT *
FROM tmptbl
但它也不起作用。
如何将我用于START WITH 子句的参数 top_ancestor_key1、top_ancestor_key2 链接到视图?
【问题讨论】:
-
为什么您尝试将
where条件添加为封闭查询,而不是在start with中指定相同的条件?您添加两个connect_by_root列的工作查询不正是您想要实现的吗? -
是的,所以通常是bind variable predicates are pushed down into the view,但当视图包含
connect by(或分析函数等)时不会。您可以尝试使用 CTE 重写您的视图吗?或者可能是物化视图。 -
@Dornaut:where 条件只是一个测试查询是否会开始评估约束的测试,因为如果连接到另一个表,这是使视图工作所必需的(但它没有) .作为一个单一的请求,我同意这种结构没有意义。
-
您使用的是哪个版本?从 19.6 开始,使用 SQL 表宏可以实现类似参数化视图的解决方案。否则你会创建一个流水线函数来实现类似的效果
-
@Andrew Sayer:我使用的是 Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 :-( 但是,如果您有 19.6 的精美解决方案,您可以为其他收到此请求的用户添加它...
标签: oracle hierarchical-data sql-view