开发者

Multiple Trees with Hierarchical Data (Left and Right values)

What issues are associated with maintaining multiple tress within a single table?

The motivation for having multiple trees is to avoid excessive updates to all nodes when inserting a node at the start. Each of the trees are completely separate entities.

Example Table:

tree_id  | id  | lft | rgt | parent_id |   various fields . . .
---------------------------------------------------------------------
1        |  1  |  1  |  4  |   NULL    |   ...
1        | 开发者_StackOverflow中文版 2  |  2  |  3  |    1      |   ...
2        |  3  |  1  |  4  |   NULL    |   ...
2        |  4  |  2  |  3  |    3      |   ...


It's very common to store multiple trees in one table, just have to make sure the values that comprise a tree are stored correctly, otherwise it'll lead to data integrity issues like nonsensical tree constructions.

Suppose we have a binary tree, (like the one in your example). If a tree was 5 depth. ((2^n)-1) = (2^5 - 1) nodes would exist or 31 rows in the database which is trivial. Even at 10 depth it's still a small amount of rows, but would be a rather ginormous tree. And so having multiple trees, X, in there would be X((2^n)-1) = rows... in the database which isn't bad. So potentially a hundred trees could exist in one table and would only be 100k rows which is relatively small.

Additionally, suppose every new tree constructed was stored in its own table, then very quickly, the database would be filled with quite a bit of tables over time to match the number of trees that exist. And it just seems like not a good idea to make extra tables that are unneeded, adds unneeded complexity in the code side to have to access these multiple tables.

Looking at your table in detail, it doesn't quite look right in terms of columns, but I'm sure that table example is just something thrown up quickly to show us what you mean.

tree_id, node_id, left_node_id, right_node_id, various_fields...

Um, be sure to index those _id fields.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜