What is the best practice for fetching a tree of nodes from database for further rendering?

前端 未结 2 1119
说谎
说谎 2021-02-02 03:32

Let\'s say we have a table with user comments. First-level comments have a reference to an article they are attached to. Deeper-level comments do not have this reference by desi

2条回答
  •  孤城傲影
    2021-02-02 04:07

    I usually recommend a design called Closure Table.

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

    I also designed this presentation: Models for Hierarchical Data with SQL and PHP. I developed a PHP app that render a tree in 0.3 seconds, from a collection of hierarchical data with 490k nodes.

    I blogged about Closure Table here: Rendering Trees with Closure Table.

    I wrote a chapter about different strategies for hierarchical data in my book, SQL Antipatterns: Avoiding the Pitfalls of Database Programming.

提交回复
热议问题