Storing trees in a database
SitePoint: Storing Hierarchical Data in a Database, by Gijs Van Tulder. The article first shows how the easy way of storing hierarchies in a database, using parent fields and a recursive PHP function to iterate up the tree. It then goes on to talk about a far more interesting alternative called “Modified Preorder Tree Traversal” where trees are first “flattened” in to a heap-like structure, then each node is stored with a pair of numbers representing that node’s position in the tree. I’d seen this somewhere before but Gijs Van Tulder’s explanation is far clearer, and comes with some good examples showing how this unconventional storage method can retrieve all of the eventual children of a node in a single query. He also talks about ways of updating the tree structure when new items are added.
Jim Biancolo - 20th June 2003 02:20 - #
Tim Parkin - 20th June 2003 09:30 - #
A reluctant thumbs down for Joe's model I'm afraid. I've tried implementing it and then had to spend a considerable amount of time teaching the developers how to use it.
The hierarchical model may not be as pure, but it is simple and easy to understand. It also has the benefit of being supported by a language extension in Oracle (CONNECT BY .. START WITH)
Finally, have you tried to re-organise a nested set? As Jim said, its fine when you have a static hierarchy, but with even a moderate level of inserts and updates it becomes hard to manage.
Andy Todd - 20th June 2003 09:53 - #
Tim Parkin - 20th June 2003 14:17 - #
Jim Biancolo - 20th June 2003 14:45 - #
There is a really excellent booklet with some unique info, called "Tree Processing in SQL", by Rozenshtein, Abramovich, and Alexandrova. It is available through http://sqlforum.com/.
This booklet has some unique techniques for optimized processing of tress in SQL, using the parent/child data arrangement.
The optimized techniques in the booklet require the use of stored procedures for traversing the tree, but it is otherwise the common parent/child data arrangement so it can be accessed directly and efficiently with INSERTs, SELECTS, etc., as well.
Definitely worth checking out if you are working with trees / graphs large enough to make you think about the alternate storage arrangements.
Jay Fienberg - 20th June 2003 21:31 - #
Sebastian Bergmann - 21st June 2003 14:12 - #
dev@ - 26th October 2004 23:28 - #