The query below is taking about a minute. I believe the poor performance is caused by the two "IN (SELECT..." clauses. I have a table of terms where one may be connected to another via the term_relationship table. These relationships can be recursive, e.g. dog is a type of mammal, mammal is a type of animal. This recursion could be any depth but probably not more than ~10 levels. I am trying to select all terms which have a (potentially recursive) relationship with type A and have a (potentially recursive) relationship with type B. I think that replacing the two "IN (SELECT..." clauses with restrictions on the outer query would improve performance but cannot figure out how to do this using the CONNECT BY clauses. Can anyone help with this?
SELECT term_name
FROM term
WHERE term_id IN
(SELECT term_id
FROM term_relationship
START WITH related_term_id = 123
CONNECT BY NOCYCLE PRIOR term_id = related_term_id)
AND term_id IN
(SELECT term_id
FROM term_relationship
START WITH related_term_id = 456
CONNECT BY NOCYCLE PRIOR term_id = related_term_id)