I am executing the following query on an H2 database through a test ran using JUnit, on a table with 3 entries:
WITH ancestors(ID, PARENT_ID) AS
(
SELECT ID, PARENT_ID FROM PEOPLE WHERE ID = <person_id>
UNION ALL
SELECT P1.ID, P1.PARENT_ID FROM PEOPLE P1, PEOPLE P2 WHERE P1.ID = P2.PARENT_ID
)
SELECT ID FROM ancestors;
The query is executed on a self-referencing table that contains people. It finds the ids of all ancestors of a person.
This for some reason runs what I would guess to be an infinite loop as the test hangs and I can see the RAM usage shoot up fast (>2GB). Why is this happening and how can I fix it?
Observations:
- this executes fine in Oracle
- if there is no PARENT_ID specified in the table it executes fine but if there is, it hangs and the RAM usage increases continuously.