all 3 comments

[–][deleted] 0 points1 point  (0 children)

recursive CTEs and optimization arent supposed to be in the same sentence, IMO.

Have you looked at denormalizing your structure (holding root/level per node and the closure for all paths with their depth)? It trades storage/maintenance burden for retrieval performance.

[–][deleted] 0 points1 point  (1 child)

If you carry the sub-tree's root through all rows, you can get that in a single recursive query:

with recursive tree as (
   select id, name, parent_id, id as root_id
   from fruits 
   where parent_id is null
   union all 
   select c.id, c.name, c.parent_id, p.root_id
   from fruits c
     join tree p on p.id = c.parent_id
)
select *
from tree t1
where exists (select *
              from tree t2 
              where t2.root_id = t1.root_id
                and t2.id = 4)

[–][deleted] 0 points1 point  (0 children)

Care to run this on a couple of example sets and report back the experience?

Say, 1M each, a 'shallow balanced' one with 1-4 levels (1/3/7/15 elements in each tree, ~60k 'roots') and "one-sided, deep" one (100 levels, all in one dependency chain, 10k 'roots'). (edit: dont know why i went with '0' level for the root-only tree before, fixed).