Optimized SQL for tree structures

前端 未结 11 907
耶瑟儿~
耶瑟儿~ 2020-11-28 21:39

How would you get tree-structured data from a database with the best performance? For example, say you have a folder-hierarchy in a database. Where the folder-database-row h

11条回答
  •  眼角桃花
    2020-11-28 22:25

    Not going to work for all situations, but for example given a comment structure:

    ID | ParentCommentID
    

    You could also store TopCommentID which represents the top most comment:

    ID | ParentCommentID | TopCommentID
    

    Where the TopCommentID and ParentCommentID are null or 0 when it's the topmost comment. For child comments, ParentCommentID points to the comment above it, and TopCommentID points to the topmost parent.

提交回复
热议问题