Implementing a hierarchical data structure in a database

后端 未结 6 606
难免孤独
难免孤独 2020-11-29 03:32

I know there are two approaches: adjacency list and nested tree. It\'s said that adjacency list can become slow to use on traversal because of numerous queries. But I don\'t

6条回答
  •  旧巷少年郎
    2020-11-29 04:28

    There are more options than just the two you mention. There are:

    • Adjacency List (the "parent_id" one almost everyone uses)
    • Nested Sets
    • Path Enumeration
    • Closure Table (aka Adjacency Relation)

    See my answer to "What is the most efficient/elegant way to parse a flat table into a tree?"

    Or a couple of books:

    • "Trees and Hierarchies in SQL for Smarties" by Joe Celko.
    • "SQL Design Patterns" by Vadim Tropashko.

提交回复
热议问题