in reply to RFC: DBIx::Tree::NestedSet

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.

Replies are listed 'Best First'.
Re^2: RFC: DBIx::Tree::NestedSet
by Hero Zzyzzx (Curate) on Jul 07, 2004 at 16:59 UTC

    Thanks for the algorithm improvements. This has been applied to the next version of DBIx::Tree::NestedSet, along with a bunch of other fixes and improvements.

    -Any sufficiently advanced technology is
    indistinguishable from doubletalk.

    My Biz