Extracted a tree structure from a flat watch
The closure table explicitly stores each ancestor-offspring relationship. This allows the use of SQL to query the offspring or ancestors efficiently.
Example:
<.> 2. Embedding set:
The nested set is to allocate an integer range for each node in the tree. The scope interval defines the location of the node in the tree level structure.
Example:<code class="language-sql">CREATE TABLE ClosureTable ( ancestor_id INT REFERENCES MyTable(id), descendant_id INT REFERENCES MyTable(id), PRIMARY KEY (ancestor_id, descendant_id) );</code>
Table:
Tree structure:
<.> 3. Administration table:
The adjacent table shows the tree as a two -row table: ID and Parent_id. Each line represents a node, and the Parent_id column points to its parent node.
<code class="language-sql">CREATE TABLE NestedSets ( id INT PRIMARY KEY, left_value INT, right_value INT );</code>
The choice of tree storage optimization technology depends on factors such as data size, query mode and database performance requirements.
<code> |-----| [0, 9] |-----| | | | | |-----| |-----| |-----| | [0, 2] | | [4, 6] | | [8, 9] | | | | | | | |-----| |-----| |-----| |-----| | [0, 1] | | [2, 3] | | [4, 5] | | [6, 7] | | | | | | | | | | [0, 0] | | [2, 2] | | [4, 4] | | [6, 6] |</code>
additional problem: Yes, use the technology described above (closing table, nested, adjacent table), there is a fundamental better method to store the tree structure in RDBMS.
The above is the detailed content of How to Efficiently Construct a Tree Hierarchy from a Flat Table and Optimize its Storage in an RDBMS?. For more information, please follow other related articles on the PHP Chinese website!