我正在研究具有层次结构的资产数据库。此外,还有一个“ReferenceAsset”表,它有效地指向资产。参考资产基本上起到了覆盖的作用,但它被选择为好像它是一个独特的新资产。设置的覆盖之一是 parent_id。
与选择层次结构相关的列:
Asset: id (primary), parent_id
Asset Reference: id (primary), asset_id (foreignkey->Asset), parent_id (always an Asset)
---EDITED 5/27----
样本相关表数据(连接后):
id | asset_id | name | parent_id | milestone | type 3 3 suit null march shape 4 4 suit_banker 3 april texture 5 5 tie null march shape 6 6 tie_red 5 march texture 7 7 tie_diamond 5 june texture -5 6 tie_red 4 march texture
id < 0(如最后一行)表示被引用的资产。引用的资产有几列被覆盖(在这种情况下,只有 parent_id 很重要)。
期望是,如果我选择了 4 月的所有资产,我应该进行二次选择以获取匹配查询的整个树分支:
所以最初查询匹配会导致:
4 4 suit_banker 3 april texture
然后在 CTE 之后,我们得到完整的层次结构,我们的结果应该是这样的(到目前为止,这是可行的)
3 3 suit null march shape 4 4 suit_banker 3 april texture -5 6 tie_red 4 march texture
你看,id:-5 的父级就在那里,但是缺少的,也就是所需要的,是被引用的资产,以及被引用的资产的父级:
5 5 tie null march shape 6 6 tie_red 5 march texture
目前我的解决方案适用于此,但仅限于单一深度的引用(而且我觉得实现非常难看)。
---已编辑---- 这是我的主要选择功能。这应该更好地展示真正复杂的地方:AssetReference。
Select A.id as id, A.id as asset_id, A.name,A.parent_id as parent_id, A.subPath, T.name as typeName, A2.name as parent_name, B.name as batchName,
L.name as locationName,AO.owner_name as ownerName, T.id as typeID,
M.name as milestoneName, A.deleted as bDeleted, 0 as reference, W.phase_name, W.status_name
FROM Asset as A Inner Join Type as T on A.type_id = T.id
Inner Join Batch as B on A.batch_id = B.id
Left Join Location L on A.location_id = L.id
Left Join Asset A2 on A.parent_id = A2.id
Left Join AssetOwner AO on A.owner_id = AO.owner_id
Left Join Milestone M on A.milestone_id = M.milestone_id
Left Join Workflow as W on W.asset_id = A.id
where A.deleted <= @showDeleted
UNION
Select -1*AR.id as id, AR.asset_id as asset_id, A.name, AR.parent_id as parent_id, A.subPath, T.name as typeName, A2.name as parent_name, B.name as batchName,
L.name as locationName,AO.owner_name as ownerName, T.id as typeID,
M.name as milestoneName, A.deleted as bDeleted, 1 as reference, NULL as phase_name, NULL as status_name
FROM Asset as A Inner Join Type as T on A.type_id = T.id
Inner Join Batch as B on A.batch_id = B.id
Left Join Location L on A.location_id = L.id
Left Join Asset A2 on AR.parent_id = A2.id
Left Join AssetOwner AO on A.owner_id = AO.owner_id
Left Join Milestone M on A.milestone_id = M.milestone_id
Inner Join AssetReference AR on AR.asset_id = A.id
where A.deleted <= @showDeleted
我有一个存储过程,它接受一个临时表(#temp)并找到层次结构的所有元素。我采用的策略是这样的:
- 将整个系统层次结构选择到一个临时表 (#treeIDs) 中,该表由每个完整树分支的逗号分隔列表表示
- 获取资产匹配查询的整个层次结构(来自#temp)
- 从层次结构中获取 Assets 指向的所有参考资产
- 解析所有参考资产的层次结构
这暂时有效,因为参考资产始终是分支上的最后一项,但如果不是,我想我会遇到麻烦。我觉得我需要一些更好的递归形式。
这是我当前的代码,它正在运行,但我并不为此感到自豪,而且我知道它并不健壮(因为它仅在引用位于底部时才有效):
步骤 1. 构建整个层次结构
;WITH Recursive_CTE AS (
SELECT Cast(id as varchar(100)) as Hierarchy, parent_id, id
FROM #assetIDs
Where parent_id is Null
UNION ALL
SELECT
CAST(parent.Hierarchy + ',' + CAST(t.id as varchar(100)) as varchar(100)) as Hierarchy, t.parent_id, t.id
FROM Recursive_CTE parent
INNER JOIN #assetIDs t ON t.parent_id = parent.id
)
Select Distinct h.id, Hierarchy as idList into #treeIDs
FROM ( Select Hierarchy, id FROM Recursive_CTE ) parent
CROSS APPLY dbo.SplitIDs(Hierarchy) as h
Step 2. 选择与查询匹配的所有资产的分支
Select DISTINCT L.id into #RelativeIDs FROM #treeIDs
CROSS APPLY dbo.SplitIDs(idList) as L
WHERE #treeIDs.id in (Select id FROM #temp)
步骤 3. 获取分支中的所有参考资产(参考资产具有负 id 值,因此 id < 0 部分)
Select asset_id INTO #REFLinks FROM #AllAssets WHERE id in
(Select #AllAssets.asset_id FROM #AllAssets Inner Join #RelativeIDs
on #AllAssets.id = #RelativeIDs.id Where #RelativeIDs.id < 0)
第 4 步。获取第 3 步中找到的任何内容的分支
Select DISTINCT L.id into #extraRelativeIDs FROM #treeIDs
CROSS APPLY dbo.SplitIDs(idList) as L
WHERE
exists (Select #REFLinks.asset_id FROM #REFLinks WHERE #REFLinks.asset_id = #treeIDs.id)
and Not Exists (select id FROM #RelativeIDs Where id = #treeIDs.id)
我试图只显示相关代码。我非常感谢任何可以帮助我找到更好解决方案的人!