I have a table reply of which structure is following:
Id name parent_id ...
1 reply1 0
2 reply2 1
3 reply3 2
4 reply4 3
5 reply5 4
This table was constructed on a hierarchical relationship(sort of parent->child), how I can get all sub replies according a id of one replies? I want use one SQL to accomplish the best performance. because really volume of replies is huge and probably one tree have more than 1000 rows.
I tried to use START WITH and CONNECT BY, but it came up with a little poor performance.
Appendix:
My rusty sql:
SELECT *
from reply
start with (parent_Id=0 AND id=?)
connect by prior Id=parent_Id
Both Id and parent_id are being indexed, “connect by” statement seems be expensive, and it causes high CPU utilization on the database side if multple “connect by” SQL runs at the same time. for example: 30 threads: It takes almost 30 minuts on executing a single query
Without an actual explain plan we can only guess.
I think your index on
parent_idis not helping here since all your “trees” start with the root 0. I would make this index composite — ie:(parent_id, id). This should speed up yourstart withclause, afterwards theidindex should be used.