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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.