编辑:我通过引入位置实体提供了附加信息,以明确我尝试使用子查询的原因
在oracle 11g数据库中,我有元素的分层结构表,最终将包含数百万行。每行都有索引的外键,指向其父级,不允许循环。 元素也有名称和类型。除此之外,还有另一个实体 - location ,类似于元素(层次结构,具有指向父+名称的外键)。 Top 元素(你的根)可以在位置(它们通过 LocationId 连接)。所以有两个实体:
位置:
元素:
现在让我们说表包含以下数据,例如:
位置:
Id | ParentId | Name
----------------------------------
100 | null | TopLocation
101 | 100 | Level1Location
102 | 101 | Level2Location
元素:
Id | LocationId | ParentId | TypeId | Name
----------------------------------------------------
1 | 102 | null | 10 | TopParent
2 | null | 1 | 11 | Level1Child
3 | null | 2 | 11 | Level2Child
我要做的是为元素编写查询,除了基本的4个元素列之外,还返回父ID,名称和类型ID的完整路径+完整路径顶部元素 位置 ID和名称。因此,如果我使用Id 3获取元素(此条件也可以通过此处未指定的多个列复杂化),则查询必须返回此信息:
Id | ParentId | TypeId | Name | IdsPath | TypeIdsPath | NamesPath | LocIdsPath | LocNamesPath
---------------------------------------------------------------------------------------------------------------------------------------------------------------
3 | 2 | 11 | Level2Child | /3/2/1 | /11/11/10 | /Level2Child/Level1Child/TopParent | /102/101/100 | /Level2Location/Level1Location/TopLocation
首先我写了oracle hierarchical query,它为 location 和元素
返回了所需的路径位置
select
SYS_CONNECT_BY_PATH(Id, '/') IdsPath,
SYS_CONNECT_BY_PATH(Name, '/') NamesPath
from
loc
where
connect_by_isleaf = 1
CONNECT BY PRIOR ParentId = e.Id
start with Id = 102
元素
select
SYS_CONNECT_BY_PATH(Id, '/') IdsPath,
SYS_CONNECT_BY_PATH(TypeId, '/') TypeIdsPath,
SYS_CONNECT_BY_PATH(Name, '/') NamesPath
from
ele
where
connect_by_isleaf = 1
CONNECT BY PRIOR ParentId = e.Id
start with Id = 3
当我想将这些查询用作基本选择中加入的子查询时,问题就出现了 - 一个不能用连接条件替换以条件开头,因为分层查询比全表扫描更新:
select
e.*,
elePath.IdsPath,
elePath.TypeIdsPath,
elePath.NamesPath,
locPath.IdsPath as LocIdsPath,
locPath.NamesPath as LocNamesPath
from
ele e
left join (
--full table scan!
select
CONNECT_BY_ROOT(Id) Id,
Id as TopEleId,
SYS_CONNECT_BY_PATH(Id, '/') IdsPath,
SYS_CONNECT_BY_PATH(TypeId, '/') TypeIdsPath,
SYS_CONNECT_BY_PATH(Name, '/') NamesPath
from ele
where
connect_by_isleaf = 1
CONNECT BY PRIOR ParentId = e.Id
) elePath on elePath.Id = e.Id
left join (
--full table scan!
select
CONNECT_BY_ROOT(Id) Id,
SYS_CONNECT_BY_PATH(Id, '/') IdsPath,
SYS_CONNECT_BY_PATH(Name, '/') NamesPath
from loc
where
connect_by_isleaf = 1
CONNECT BY PRIOR ParentId = e.Id
) locPath on locPath.Id = elePath.TopEleId
where
e.Id = 3
我也不能scalar subquery,因为查询必须返回多个路径,而不仅仅是一个路径。有什么建议?我是否正朝着正确的方向前进,还是应该向元素表添加一些字段并缓存我需要的所有路径? (他们不会经常更新)
谢谢!
答案 0 :(得分:3)
您可以反向遍历层次结构,只需使用connect_by_root()
运算符来获取根行的列值。
clear screen;
column IdPath format a11;
column TypeIdPathformat a11
column NamePath format a35;
with t1(id1, parent_id, type_id, Name1) as(
select 1, null, 10, 'TopParent' from dual union all
select 2, 1 , 11, 'Level1Child' from dual union all
select 3, 2 , 11, 'Level2Child' from dual
)
select connect_by_root(id1) as id1
, connect_by_root(parent_id) as ParentId
, connect_by_root(type_id) as Typeid
, connect_by_root(name1) as name1
, sys_connect_by_path(id1, '/') as IdPath
, sys_connect_by_path(type_id, '/') as TypeIdPath
, sys_connect_by_path(name1, '/') as NamePath
from t1
where connect_by_isleaf = 1
start with id1 = 3
connect by id1 = prior parent_id
结果:
id1 ParentId TypeId Name1 IdPath TypeIdPath NamePath
---------------------------------------------------------------------------
3 2 11 Level2Child /3/2/1 /11/11/10 /Level2Child/Level1Child/TopParent
编辑#1
获得所需输出的一种方法是使用标量子查询:
with Locations(Id1, ParentId, Name1) as(
select 100, null, 'TopLocation' from dual union all
select 101, 100 , 'Level1Location' from dual union all
select 102, 101 , 'Level2Location' from dual
),
elements(id1, LocationId, parent_id, type_id, Name1) as(
select 1, 102, null, 10, 'TopParent' from dual union all
select 2, null, 1 , 11, 'Level1Child' from dual union all
select 3, null, 2 , 11, 'Level2Child' from dual
)
select e.*
, (select sys_connect_by_path(l.id1, '/')
from locations l
where connect_by_isleaf = 1
start with l.id1 = e.locationid
connect by l.id1 = prior parentid) as LocIdPath
, (select sys_connect_by_path(l.name1, '/')
from locations l
where connect_by_isleaf = 1
start with l.id1 = e.locationid
connect by l.id1 = prior parentid) as LocNamePath
from ( select connect_by_root(id1) as id1
, connect_by_root(parent_id) as ParentId
, connect_by_root(type_id) as Typeid
, connect_by_root(name1) as name1
, sys_connect_by_path(id1, '/') as IdPath
, sys_connect_by_path(type_id, '/') as TypeIdPath
, sys_connect_by_path(name1, '/') as NamePath
, locationid
from elements
where connect_by_isleaf = 1
start with id1 = 3
connect by id1 = prior parent_id ) e
结果:
ID1 PARENTID TYPEID NAME1 IDPATH TYPEIDPATH NAMEPATH LOCATIONID LOCIDPATH LOCNAMEPATH
---------- ---------- ----------- ----------- ----------- ----------------------------------- ---------- ------------- -------------------------------------------
3 2 11 Level2Child /3/2/1 /11/11/10 /Level2Child/Level1Child/TopParent 102 /102/101/100 /Level2Location/Level1Location/TopLocation