What exactly are 'best-practices' to save Composite designs inside a Relational Database?
We've been using Modified Preorder Tree Traversal. This is quick to construct the entire tree, but very slow to place or remove new nodes (all right and left values have to be modified). Also querying the kids of the node is difficult and incredibly slow.
Another factor we observed is you need to make certain the tree does not get untidy. You'll need transaction locks, otherwise the right and left values could possibly get corrupt, and fixing a corrupt left right tree isn't a simple job.
It will work excellent however, the Modified Preorder Tree Traversal, but I'm wondering if you will find better options.
While finding all descendents of the row with MPTT is fast, finding all children could be slow. It is important to have the ability to fix that with the addition of a
parent_id area for your table that records (yes, redundantly) parents from the row. Then your search becomes:
SELECT * FROM tbl WHERE parent_id = z
parent_id consists of redundant information, potentially denormalizing your table -- consider any place/update/remove already requires global changes, keeping
parent_id up-to-date is not much extra to pay for. You can alternatively make use of a
level area that records the vertical degree of the row, although that's actually more prone to change under certain kinds of changes (e.g. moving a subtree to another reason for the tree).
The common link-to-parent representation (i.e. just getting
parent_id with no
right_pos), is obviously faster for place/update-heavy workloads, however the only queries it may answer effectively are "Discover the parent of X" and "Discover the kids of X." Most workloads involve a lot more reading through than writing, so usually MPTT is faster overall -- but possibly inside your situation you have to consider moving ("back") to link-to-parent?
The easiest method to store hierakial data inside a database I've heard is by using a string attribute in which the submissions are their email list of parents separated by, say colons.
Fundamental essentials two primary models:
- The Adjacency List Model
- The Nested Set Model
Both can perform exactly the same factor, but you will find a couple of little variations.
The Adjacency List Model causes it to be simple to alter trees, it causes it to be harder to retrieve them.
The Nested Set Model causes it to be super easy to retrieve a whole branch/tree. This model also naturally stores an order of nodes. The sql instructions to change the tree is a little harder to understand than in the other model.
When I described you before: the application needs The Nested Set Model. Come across my desk if you think it's too complicated. I'll explain it once again.