I have a table with the columns
parent_key1, parent_key2, child_key1, child_key2
definig trees through the connection of two pairs of parameters.
The table is quite large, and it contains thousands of root objects, that is parents that do not occur as a child; so to say the table does not contain a tree but rather a forest. This is why the queries here do not work.
I want to get the tree members starting with top_ancestor_key1 and top_ancestor_key2.
For a procedure, I can define the two parameters :top_ancestor_key1 and :top_ancestor_key2, the code
SELECT parent_key1, parent_key2, child_key1, child_key2, level
FROM genealogy
START WITH parent_key1 = :top_ancestor_key1, parent_key2 = :top_ancestor_key2,
CONNECT BY parent_key1 = PRIOR child_key1 AND parent_key2 = PRIOR child_key2
works well.
Now I would like to create a view «ancestors_resolved» with the columns
top_ancestor_key1, top_ancestor_key2, parent_key1, parent_key2, child_key1, child_key2 [, level]
that I could use the result for joins on top_ancestor_key1 and top_ancestor_key2
I have tried
--CREATE View ancestors_resolved AS
SELECT connect_by_root parent_key1 as top_ancestor_key1, connect_by_root parent_key2 as top_ancestor_key2, parent_key1, parent_key2, child_key1, child_key2, level
FROM genealogy
CONNECT BY parent_key1 = PRIOR child_key1 AND parent_key2 = PRIOR child_key2
however, the enclosing query
SELECT * FROM
(
SELECT connect_by_root parent_key1 as top_ancestor_key1, connect_by_root parent_key2 as top_ancestor_key2, parent_key1, parent_key2, child_key1, child_key2, level
FROM genealogy
CONNECT BY parent_key1 = PRIOR child_key1 AND parent_key2 = PRIOR child_key2
)
WHERE top_ancestor_key1='grandpa' AND top_ancestor_key2 = 5
runs into timeout; it seems like oracle trying to build all trees before evaluating the Parameters.
I also tried
WITH tmptbl (parent_key1, parent_key2, child_key1, child_key2) as (
SELECT parent_key1, parent_key2, child_key1, child_key2
FROM genealogy
UNION ALL
SELECT tmptbl.parent_key1, tmptbl.parent_key2, tmptbl.child_key1, tmptbl.child_key2
FROM tmptbl
INNER JOIN genealogy x on x.child_key1 = tmptbl.parent_key1 and x.child_key2 = tmptbl.parent_key2 and x.child_key1 != x.parent_key1 and x.child_key2 != x.parent_key2
)
SELECT *
FROM tmptbl
but it did not work either.
How do I link the parameters top_ancestor_key1, top_ancestor_key2 I use for START WITH clause to a view?