请输入您要查询的百科知识:

 

词条 Nested set model
释义

  1. Motivation

  2. The technique

  3. Example

  4. Performance

  5. Drawbacks

  6. Variations

  7. See also

  8. References

  9. External links

The nested set model is a particular technique for representing nested sets (also known as trees or hierarchies) in relational databases.

The term was apparently{{clarify|date=October 2018}} introduced by Joe Celko; others describe the same technique using different terms.[1][2]

Motivation

The standard relational algebra and relational calculus, and the SQL operations based on them, are unable to express directly all desirable operations on hierarchies. The nested set model is a solution to that problem.

An alternative solution is the expression of the hierarchy as a parent-child relation. Celko called this the adjacency list model. If the hierarchy can have arbitrary depth, the adjacency list model does not allow the expression of operations such as comparing the contents of hierarchies of two elements, or determining whether an element is somewhere in the subhierarchy of another element. When the hierarchy is of fixed or bounded depth, the operations are possible, but expensive, due to the necessity of performing one relational join per level. This is often known as the bill of materials problem.{{citation needed|date=November 2011}}

Hierarchies may be expressed easily by switching to a graph database. Alternatively, several resolutions exist for the relational model and are available as a workaround in some relational database management systems:

  • support for a dedicated hierarchy data type, such as in SQL's hierarchical query facility;
  • extending the relational language with hierarchy manipulations, such as in the nested relational algebra.
  • extending the relational language with transitive closure, such as SQL's CONNECT statement; this allows a parent-child relation to be used, but execution remains expensive;
  • the queries can be expressed in a language that supports iteration and is wrapped around the relational operations, such as PL/SQL, T-SQL or a general-purpose programming language

When these solutions are not available or not feasible, another approach must be taken.

The technique

The nested set model is to number the nodes according to a [https://www.techiedelight.com/arrival-departure-time-vertices-dfs/ traversal (DFS arrival and departure of vertices)] , which visits each node twice, assigning numbers in the order of visiting, and at both visits. This leaves two numbers for each node, which are stored as two attributes. Querying becomes inexpensive: hierarchy membership can be tested by comparing these numbers. Updating requires renumbering and is therefore expensive. Refinements that use rational numbers instead of integers can avoid renumbering, and so are faster to update, although much more complicated.[3]

Example

In a clothing store catalog, clothing may be categorised according to the hierarchy given on the left:

Node Left Right
Clothing 1 22
Men's 2 9
Women's 10 21
Suits 3 8
Slacks 4 5
Jackets 6 7
Dresses 11 16
Skirts 17 18
Blouses 19 20
Evening Gowns 12 13
Sun Dresses 14 15
The resulting representation

The "Clothing" category, with the highest position in the hierarchy, encompasses all subordinating categories. It is therefore given left and right domain values of 1 and 22, the latter value being the double of the total number of nodes being represented. The next hierarchical level contains "Men's" and "Women's", both containing levels within themselves that must be accounted for. Each level's data node is assigned left and right domain values according to the number of sublevels contained within, as shown in the table data.

Performance

Queries using nested sets can be expected to be faster than queries using a stored procedure to traverse an adjacency list, and so are the faster option for databases which lack native recursive query constructs, such as MySQL.[4] However, recursive SQL queries can be expected to perform comparably for 'find immediate descendants' queries, and much faster for other depth search queries, and so are the faster option for databases which provide them, such as PostgreSQL,[5] Oracle,[6] and Microsoft SQL Server.[7]

Drawbacks

The use case for a dynamic endless database tree hierarchy is rare. The Nested Set model is appropriate where the tree element and one or two attributes are the only data, but is a poor choice when more complex relational data exists for the elements in the tree. Given an arbitrary starting depth for a category of 'Vehicles' and a child of 'Cars' with a child of 'Mercedes', a foreign key table relationship must be established unless the tree table is natively non-normalized. Attributes of a newly created tree item may not share all attributes with a parent, child or even a sibling. If a foreign key table is established for a table of 'Plants' attributes, no integrity is given to the child attribute data of 'Trees' and its child 'Oak'. Therefore, in each case of an item inserted into the tree, a foreign key table of the item's attributes must be created for all but the most trivial of use cases.

If the tree isn't expected to change often, a properly normalized hierarchy of attribute tables can be created in the initial design of a system, leading to simpler, more portable SQL statements; specifically ones that don't require an arbitrary number of runtime, programmatically created or deleted tables for changes to the tree. For more complex systems, hierarchy can be developed through relational models rather than an implicit numeric tree structure. Depth of an item is simply another attribute rather than the basis for an entire DB architecture. As stated in SQL Antipatterns:[8]

Nested Sets is a clever solution – maybe too clever. It also fails to support referential integrity. It’s best used when you need to query a tree more frequently than you need to modify the tree.[9]

The model doesn't allow for multiple parent categories. For example, an 'Oak' could be a child of 'Tree-Type', but also 'Wood-Type'. An additional tagging or taxonomy has to be established to accommodate this, again leading to a design more complex than a straightforward fixed model.

Nested sets are very slow for inserts because it requires updating left and right domain values for all records in the table after the insert. This can cause a lot of database stress as many rows are rewritten and indexes rebuilt. However, if it is possible to store a forest of small trees in table instead of a single big tree, the overhead may be significantly reduced, since only one small tree must be updated.

The nested interval model does not suffer from this problem, but is more complex to implement, and is not as well known. It still suffers from the relational foreign-key table problem. The nested interval model stores the position of the nodes as rational numbers expressed as quotients (n/d).  

Variations

Using the nested set model as described above has some performance limitations during certain tree traversal operations. For example, trying to find the immediate child nodes given a parent node requires pruning the subtree to a specific level as in the following SQL code example:

SELECT Child.Node, Child.Left, Child.Right

FROM Tree as Parent, Tree as Child

WHERE

Child.Left BETWEEN Parent.Left AND Parent.Right

AND NOT EXISTS ( -- No Middle Node

SELECT *

FROM Tree as Mid

WHERE Mid.Left BETWEEN Parent.Left AND Parent.Right

AND Mid.Node NOT IN (Parent.Node AND Child.Node)

)

AND Parent.Left = 1 -- Given Parent Node Left Index

Or, equivalently:

SELECT DISTINCT Child.Node, Child.Left, Child.Right

FROM Tree as Child, Tree as Parent

WHERE Parent.Left < Child.Left AND Parent.Right > Child.Right -- associate Child Nodes with ancestors

GROUP BY Child.Node, Child.Left, Child.Right

HAVING max(Parent.Left) = 1 -- Subset for those with the given Parent Node as the nearest ancestor

The query will be more complicated when searching for children more than one level deep. To overcome this limitation and simplify tree traversal an additional column is added to the model to maintain the depth of a node within a tree.

Node Left Right Depth
Clothing 1 22 0
Men's 2 9 1
Women's 10 21 1
Suits 3 8 2
Slacks 4 5 3
Jackets 6 7 3
Dresses 11 16 2
Skirts 17 18 2
Blouses 19 20 2
Evening Gowns 12 13 3
Sun Dresses 14 15 3
The resulting representation

In this model, finding the immediate children given a parent node can be accomplished with the following SQL code:

SELECT Child.Node, Child.Left, Child.Right

FROM Tree as Child, Tree as Parent

WHERE

Child.Depth = Parent.Depth + 1

AND Child.Left > Parent.Left

AND Child.Right < Parent.Right

AND Parent.Left = 1 -- Given Parent Node Left Index

See also

  • Tree traversal
  • Tree (data structure)

References

1. ^Recursive Dimension Hierarchies: The Relational Taboo by Michael Kamfonas originally published at the Relational Journal in 1992
2. ^Storing Hierarchical Data in a Database: Modified Pre-order Tree Traversal, by Gijs van Tulder, at articles.sitepoint.com
3. ^{{cite arXiv |eprint=0806.3115 | first= Daniel|last = Hazel | title = Using rational numbers to key nested sets}}
4. ^{{Citation | title= Adjacency list vs. nested sets: MySQL | author= Quassnoi | date = 29 September 2009 | periodical = Explain Extended | url = https://explainextended.com/2009/09/29/adjacency-list-vs-nested-sets-mysql/ | accessdate = 11 December 2010}}
5. ^{{Citation | title= Adjacency list vs. nested sets: PostgreSQL | author= Quassnoi | date = 24 September 2009 | periodical = Explain Extended | url = http://explainextended.com/2009/09/24/adjacency-list-vs-nested-sets-postgresql/ | accessdate = 11 December 2010}}
6. ^{{Citation | title= Adjacency list vs. nested sets: Oracle | author= Quassnoi | date = 28 September 2009 | periodical = Explain Extended | url = http://explainextended.com/2009/09/28/adjacency-list-vs-nested-sets-oracle/ | accessdate = 11 December 2010}}
7. ^{{Citation | title= Adjacency list vs. nested sets: SQL Server | author= Quassnoi | date = 25 September 2009 | periodical = Explain Extended | url = http://explainextended.com/2009/09/25/adjacency-list-vs-nested-sets-sql-server/ | accessdate = 11 December 2010}}
8. ^{{cite book|last1=Bill|first1=Karwin|title=SQL Antipatterns|date=2010-06-17|pages=328|url=https://pragprog.com/book/bksqla/sql-antipatterns}}
9. ^{{cite book|last1=Bill|first1=Karwin|title=SQL Antipatterns|page=44}}

External links

  • Troels' links to Hierarchical data in RDBMSs
  • Managing hierarchical data in relational databases
  • PHP PEAR Implementation for Nested Sets – by Daniel Khan
  • Transform any Adjacency List to Nested Sets using MySQL stored procedures
  • [https://github.com/previousnext/nested-set PHP Doctrine DBAL implementation for Nested Sets] – by PreviousNext
  • [https://github.com/Vince0931/NestedSet R Nested Set] – Nested Set example in R
{{DEFAULTSORT:Nested Set Model}}

1 : Database theory

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/11/15 11:25:36