I don’t know if this will fit your problem, but one way to store hierarchies in databases is with a quick “give me everything from this node and down function” to save the “path”.
For example, for a tree that looks like this:
+-- b | a --+ +-- d | | +-- c --+ | +-- e
you would save the lines as follows, assuming that the letter in the above tree is the "id" of each line:
id path aa ba*b ca*c da*c*d ea*c*e
To find all the descendants of a particular node, you should make a query "STARTSWITH" in the path column, i.e. all nodes with a path starting with a*c*
To find out if a particular node is a descendant of another node, you will see if the longest path was run with the shortest path.
So for example:
- e is a descendant of a, since
a*c*e
starts with a
- d is a descendant of c since
a*c*d
starts with a*c
Would this be useful in your instance?
Lasse Vågsæther Karlsen
source share