Implementing a hierarchical data structure in a database

后端 未结 6 604
难免孤独
难免孤独 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:15

    The other approach is called "nested set", I think, not "nested tree".

    Anyway, a good thing about a site map is that you might know its maximum depth. I think that the problem with the adjacency model is that the corresponding SQL works on one level at a time, so if you have 'n' levels then you need a loop of 'n' SQL statements ... but I think (I'm not sure) that if you know the maximum 'n' in advance then you can code the corresponding fixed-number-of-multiple-levels SQL.

    0.3 seconds sounds to me like a very long time to figure 200 pages, so that's probably OK.

    Also a site map isn't updated very often; so even if it does take a long time to retrieve from SQL, you can probably cache the retrieved/calculated tree in RAM.

    ALternatively, instead of worrying about the SQL to build a tree, you could just store it as simply as possible (as adjacency list), retrieve it from the database as a simple set of rows, and build the tree in RAM (using loops in your high-level programming language) instead of using loops in SQL to build the tree using SQL statements.

提交回复
热议问题