NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.
All the Perl that's Practical to Extract and Report
Stories, comments, journals, and other submissions on use Perl; are Copyright 1998-2006, their respective owners.
Cute Tricks and Premature Optimizations (Score:2)
While this technique works fine with a static tree, it makes tree transformations hideously difficult. Imagine the pain that comes from moving node 12 to be a sibling of node 5, or removing node 5 and consequently promoting their children up a level.
The other technique of using a single integer column to store parent_id is the most flexible, even if the SQL is slightly hairier.
Alternatively, you can select the entire subtree as a two pass operation. First, (recursively) find all of the children of node 5. Next, build a UNION query of all parent nodes that are part of the subtree rooted at node 5. If you build your union query properly, the nodes also come back in-order.
Reply to This
Re:Cute Tricks and Premature Optimizations (Score:1)
-Dom
Re:Cute Tricks and Premature Optimizations (Score:2)
Re:Cute Tricks and Premature Optimizations (Score:2)
I personally don't think that the "ton of SELECTs" is all that horrible. That's probably my personal style though.
It starts out with something like this:
...which returns a list of IDs. That list of IDs then goes into a new SQL statement. The IDs are also pushed into a list that contains all IDs returned from your per-leve
Re:Cute Tricks and Premature Optimizations (Score:2)
I've written code as the one you describe dozens of times and I must say that it certainly was "fast enough". I'd be worried if I had very deep trees though, as that would be when it could become costly.
But then why store a tree in an RDB? Wouldn't XPath be a *lot* more pleasant to access random things in a tree? ;)
-- Robin Berjon [berjon.com]