Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Im not sure how you have implemented the drop/delete node, but it seems that if i would drop a node with 1000 descendants it would cost me 1000 selects, 1000 deletes and 1000 updates.

This is is usually done with two statements, one delete and one update.

If you have this tree and you would like to drop node C and it's descendants:

                    |
                  1 A 12
  /-----------------+-----------------\
  |                 |                 |
2 B 3             4 C 9            10 D 11
              /-----------\
              |           |
            5 E 6       7 F 8

  DELETE
    FROM tree
   WHERE lft BETWEEN :lft AND :rgt

:lft = 4
:rgt = 9


  UPDATE tree
     SET lft = CASE
                 WHEN lft > :lft THEN lft - :gaps
                 ELSE lft
               END,
         rgt = CASE
                 WHEN rgt > :rgt THEN rgt - :gaps
                 ELSE rgt
               END
   WHERE rgt > :lft

:gaps = 9 - 4 + 1  (:rgt - :lft + 1)
:lft  = 4
:rgt  = 9

And the result:

        |
      1 A 6
  /-----------\
  |           |
2 B 3       4 D 5

The Nested Set algorithm is one of my favorites, it's very efficient and portable. It can handle large trees with out problems.


In reply to Re: RFC: DBIx::Tree::NestedSet by Hansen
in thread RFC: DBIx::Tree::NestedSet by Hero Zzyzzx

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-24 06:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found