Efficient use of Teradata Recursive Queries
Teradata Recursive Queries
Each recursive query consists of 2 or 3 parts, depending on which of above approaches is used:
- The seed statement is the initial query which is executed.
- The recursive statement is the repeating query which is executed until no further rows are returned. To avoid infinite loops, we usually add a termination condition to the recurrent query. Without a termination condition, the user may run out of spool space.
- The final query returns the result of the seed query and all iterations of the recursive query.
Creating a recursive query using the WITH RECURSIVE clause:
WITH RECURSIVE <query>
Creating a recursive query using a RECURSIVE VIEW:
CREATE RECURSIVE VIEW <view>
To get a better understanding of how recursive queries work on Teradata, I prepared a common problem we want to solve: Finding the shortest paths in a graph.
Shortest Path – The Recursive Solution
We will store all available legs in below table:
CREATE SET TABLE Leg
From_Id INTEGER NOT NULL,
To_Id INTEGER NOT NULL
PRIMARY INDEX (From_Id);
/* Example Graph */
INSERT into Leg VALUES (‘1′,'2');
INSERT into Leg VALUES (‘2′,'1');
INSERT into Leg VALUES (‘2′,'3');
INSERT into Leg VALUES (‘2′,'7');
INSERT into Leg VALUES (‘3′,'2');
INSERT into Leg VALUES (‘3′,'4');
INSERT into Leg VALUES (‘3′,'5');
INSERT into Leg VALUES (‘3′,'6');
INSERT into Leg VALUES (‘6′,'7');
INSERT into Leg VALUES (‘6′,'3');
INSERT into Leg VALUES (‘7′,'2');
INSERT into Leg VALUES (‘7′,'6');
INSERT into Leg VALUES (‘7′,'8');
INSERT into Leg VALUES (‘7′,'9');
INSERT into Leg VALUES (‘8′,'7');
INSERT into Leg VALUES (‘9′,'7');
INSERT into Leg VALUES (‘5′,'3');
INSERT into Leg VALUES (‘4′,'3');
The solution with a recursive query:
WITH RECURSIVE ThePath
(From_Id, To_Id, Path, TheLength) AS
,(TRIM(From_Id) || ‘,' || TRIM(To_Id)) (VARCHAR(512)) AS Path
, 1 AS TheLength
From_Id = ‘1'
,ThePath.Path || ‘,' || TRIM(t01.To_Id)
,ThePath.TheLength + 1 AS TheLength
t01.From_Id = ThePath.To_Id
WHERE POSITION(‘,' || TRIM(t01.To_Id) || ‘,' IN ‘,' || ThePath.Path || ‘,') = 0
— Above WHERE condition ensures that we do not revisit a node a second time!
AND ThePath.TheLength <= 100
— Avoid out of spool situations, put a fixed stop after 100 recursions!
/* Below statement ensures that if there are multiple routes between two nodes, one of
the minimum numbers of stops are chosen */
QUALIFY ROW_NUMBER() OVER (PARTITION BY From_Id, To_Id ORDER BY TheLength, Path) = 1
ORDER BY 1,4,3;
Here is the result set of the query, showing all minimum distance routes from node 1
Shortest Path – The Non-Recursive Solution
We will now compare the recursive query with a solution written as Stored Procedure:
CREATE SET TABLE Route
From_Id INTEGER NOT NULL,
To_Id INTEGER NOT NULL,
) PRIMARY INDEX (From_Id);
REPLACE PROCEDURE ThePath()
DYNAMIC RESULT SETS
DELETE FROM ShortestPath;
INSERT INTO ShortestPath
(TRIM(From_Hub_Id) || ‘,' || TRIM(To_Hub_Id)) (VARCHAR(512)) AS Path,
1 AS TheLength
WHERE From_id = ‘1';
WHILE ACTIVITY_COUNT > 0 DO
INSERT INTO Route
t02.Path || ‘,' || TRIM(t01.To_Id)
t02.TheLength + 1
FROM Leg t01, Route t02
t01.From_Id = t02.To_Id
AND t01.To_Id <> ‘1'
AND t02.TheLength =
(SELECT MAX(TheLength) FROM Route)
AND t01.To_Id NOT IN
(SELECT To_Id FROM Route)
AND B.TheLength < 200
QUALIFY ROW_NUMBER() OVER (PARTITION BY t01.To_Id ORDER BY t02.Path) = 1;
DECLARE mycursor CURSOR WITH RETURN ONLY FOR
SELECT * FROM Route ORDER BY 4, 3;
In general, it can't be said which solution is faster or slower. It depends in how data structures accessed look like.
In this specific example, the stored procedure consumes fewer IOs and CPU that the recursive query.
There are several reasons:
- Our stored procedure keeps a routes table of all visited nodes, while the recursive query might revisit the same node several times.
- The recursive query continues to iterate even after the shortest path between two nodes has already been found. Running additional recursive steps increases the spool usage quickly.