Slow hierarchical query
783568Feb 9 2011 — edited Feb 9 2011Hello
I wrote a query that enables the user to filter the displayed data according to several parameters, one of the parameters (b) is hierarchical e.g. every item (except from the root item) has a father and several sons, the hierarchy is managed by a specific table (LINKS), I need that when the user select b the result will also include all its descendants.
This is the query:
SELECT *
FROM R
INNER JOIN RT ON R.A = RT.A
WHERE (R.B IN (select sub_id
from LINKS start with father_id = :id
connect by prior sub_id= father_id)
or R.B = :id or :id = 0)
AND (R.A= :a OR :a=0)
ORDER BY R.B, R.A
The problem is that the query is so slow it makes the application to stuck, when I omitted the line: "or R.B = :id or :id = 0" it did'nt stuck but this line is necessary since the user can also leave the filter field empty or try to filter to b itself and not to its descendants.
Is there a way to improve this query performance or to write it better?