in reply to Re^3: message board thread quandary
in thread message board thread quandary

Your comparison is incomplete.
It is one way to do it. With the approach you suggested, it means going through this process:
  1. fetch an entry
  2. find any children
  3. for each child, call step 1

And with an additional table, the steps are?

  1. fetch an entry
  2. fetch all descendants of that entry (gives a collection)
  3. build a tree doing ... what exactly? Probably a select to get its immediate parent.

There. :-)

update - having read the Nested Set Model of Trees - well, that's one way to do it. You have to jump through hoops still to get your data into a tree structure suitable to be displayed in a TT template. But anyways, trading simplicity and additional database calls for just one call (which I doubt would satisfy the OP's requirements) and more code complexity is something I would do only if I had proven the fact that multiple queries are the bottleneck in that application.

update 2: Thinking about it, building a tree given the parent_id in each record would probably be pretty simple, so there's no point discussing your improvement.

cheers,

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^5: message board thread quandary
by clinton (Priest) on Sep 02, 2007 at 15:48 UTC
    Good point :)

    However, by selecting all the descendants at once, you do have all the information to build a tree within your program. I'm guessing here, but I would think that doing it in memory would still be faster than doing it with multiple selects.

    I would concede to those with more experience though :)

    Clint

    Update I'm assuming that by selecting all of the objects, you are also receiving the parent_id of each object, which would give you the information to build the tree.