Managing hierarchies in SQL: MPTT/nested sets vs adjacency lists vs storing paths

后端 未结 2 1025
余生分开走
余生分开走 2020-12-24 09:09

For a while now I\'ve been wrestling with how best to handle hierarchies in SQL. Frustrated by the limitations of adjacency lists and the complexity of MPTT/nested sets, I b

2条回答
  •  不思量自难忘°
    2020-12-24 09:36

    It problem with your conclusion is that it ignores most of the issues involved in working with trees.

    By reducing the validity of a technique to the "number of calls" you effectively ignore all of the issues which well understood data structures and algorithms attempt to solve; that is, fastest execution and low memory and resource foot print.

    The "number of calls" to an SQL server may seem like a good metric to use ("look ma less code"), but if the result is a program which never finishes, runs slowly, or takes up to much space, it is in fact a useless metric.

    By storing the path with every node you are not creating a tree data structure. Instead you are creating a list. Any operation which a tree is designed to optimize is lost.

    This might be hard to see with small date sets (and in many cases of small trees a list is better), try some examples on data sets of size 500, 1000, 10k -- You will quickly see why storing the whole path is not a good idea.

提交回复
热议问题