I've been tasked with migrating a script that traverses a hierarchy and expands it. Firstly the script is running extremely slow and secondly we are moving into a far more controlled server so I need to eliminate functions. I was wondering if someone could perhaps assist in integrating what the function is doing in the second statement and calling the entire script inside the selection statement of the first script.
I understand that split between the two may be far better performance wise however this is the only function that exists and the only select statement that is using it so I would much rather prefer to integrate the two rather than go through the process of getting it approved and added. Secondly, if anyone could see a more optimal way to achieve this it would be great and I am open to suggestions, keeping in mind this goes about 11 levels deep.
The first part of the script is the select statement where the function is called and obviously returned to a table:
DECLARE @RootNode INT = 1
DECLARE @Level1 INT = 2
DECLARE @Level2 INT = 3
DECLARE @Level3 INT = 4
DECLARE @Level4 INT = 5
TRUNCATE TABLE [...].[Hierarchy]
--
INSERT INTO [...].[Hierarchy]
SELECT Nodes.NodeId,
NodeTypeValues.Value AS HierarchyValue,
(select NodeTypeValue from [...].[Function_GetTheParentNodesForTheSelectedNodeType] (abc.NodeId, @RootNode)) AS RootLevel,
(select NodeTypeValue from [...].[Function_GetTheParentNodesForTheSelectedNodeType] (abc.NodeId, @Level1)) AS Level1,
(select NodeTypeValue from [...].[Function_GetTheParentNodesForTheSelectedNodeType] (abc.NodeId, @Level2)) AS Level2,
(select NodeTypeValue from [...].[Function_GetTheParentNodesForTheSelectedNodeType] (abc.NodeId, @Level3)) AS Level3,
(select NodeTypeValue from [...].[Function_GetTheParentNodesForTheSelectedNodeType] (abc.NodeId, @Level4)) AS Level4
--Level 5...
--Level 6...
--Level 7...
FROM [...].[Nodes] Nodes
INNER JOIN [...].NodeTypes NodeTypes ON NodeTypes.NodeTypeId = Nodes.NodeTypeId
INNER JOIN [...].NodeTypeValues NodeTypeValues ON NodeTypeValues.NodeTypeValueId = Nodes.NodeTypeValueId
WHERE NodeTypes.HierarchyTypeId = 1
The second part is the actual function that is being called, the function is meant to traverse and return a tabled result back to the main query for storage:
FUNCTION [...].[Function_GetTheParentNodesForTheSelectedNodeType]
( @NodeId int,
@NodeTypeId int
)
RETURNS
@ReturnData TABLE
(
NodeTypeValue NVARCHAR(100),
NodeId INT
)
AS
BEGIN
WITH NodeSubTreesUpwards AS
(
SELECT SubRootNode.NodeId AS SubRootNodeId,
SubRootNode.*,
NULL AS ChildNodeId,
0 AS HierarchyLevel
FROM [...].[Nodes] AS SubRootNode
WHERE SubRootNode.NodeId = @NodeId
UNION ALL
SELECT NodeSubTreesUpwards.SubRootNodeId,
ParentNode.*,
Parent.ChildNodeId, (NodeSubTreesUpwards.HierarchyLevel) - 1 AS HierarchyLevel
FROM NodeSubTreesUpwards
INNER JOIN [...].[ParentChildNodes] AS Parent ON Parent.ChildNodeId = NodeSubTreesUpwards.NodeId
INNER JOIN [...].[Nodes] AS ParentNode ON ParentNode.NodeId = Parent.ParentNodeId
)
INSERT INTO @ReturnData
SELECT TOP 1 NodeTypeValues.Value, NodeSubTreesUpwards.NodeId
FROM NodeSubTreesUpwards NodeSubTreesUpwards
INNER JOIN [...].[NodeTypes] NodeType ON NodeType.NodeTypeId = n.NodeTypeId
INNER JOIN [...].[NodeTypeValues] NodeTypeValues ON NodeTypeValues.NodeTypeValueId = n.NodeTypeValueId
WHERE NodeType.NodeTypeId = @NodeTypeId
RETURN
I have really attempted to split this out but been struggling to do so, I'm most likely missing something stupid or its purely just not understanding the process of creating a hierarchy, I've sat on this for a day or two now. I would be more than happy to use the same function just without calling it and rather doing it in the main select statement in place of the function being called but not sure if due to the recursion this will be an issue?