开发者

How can I get the best Hierarchical Query performance in oracle

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_id is 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 your start with clause, afterwards the id index should be used.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜