I am exploring new (faster) ways to store/query hierarchical data in my database.
I have stumbled upon https://www.sqlservercentral.com/articles/hierarchies-on-steroids-1-convert-an-adjacency-list-to-nested-sets membership from another post which has interesting claims in terms of speed for my dataset. However the code was written for SQL Server and I am using Oracle (19c), which cannot be changed.
I am trying to convert the code to Oracle but I'm blocked with the BINARY
types as the corresponding RAW
in Oracle don't seem to match what the author is doing.
So far I have this, which is not working because of the SortPath:
create table hierarchy
as
WITH cteBuildPath(object_id, parent_object_id, HLevel, SortPath) AS
(
SELECT anchor.object_id,
anchor.parent_object_id,
1 HLevel,
CAST(CAST(anchor.object_id AS RAW(4)) AS LONG RAW) SortPath
FROM playground anchor
WHERE parent_object_id IS NULL
UNION ALL
SELECT recur.object_id,
recur.parent_object_id,
cte.HLevel + 1 HLevel,
CAST(cte.SortPath + CAST(Recur.object_id AS RAW(4)) AS LONG RAW) SortPath
FROM playground recur
INNER JOIN cteBuildPath cte
ON cte.object_id = recur.parent_object_id
)
SELECT NVL(sorted.object_id,0) object_id,
sorted.parent_object_id,
NVL(sorted.HLevel,0) HLevel,
NVL(CAST(0 AS INT),0) LeftBower, --Place holder
NVL(CAST(0 AS INT),0) RightBower, --Place holder
ROW_NUMBER() OVER (ORDER BY sorted.SortPath) NodeNumber,
NVL(CAST(0 AS INT),0) NodeCount, --Place holder
NVL(sorted.SortPath,sorted.SortPath) SortPath
FROM cteBuildPath sorted
;
I also did not find how to translate the WITH (MAXRECURSION 1000)
of the original article.