Relational databases can handle graph structures. Some of them can even process them moderately elegantly (as elegantly as a relational database!).
The key to general graph processing in relational databases is the recursive common table expression (RCTE), which basically allows you to iteratively (not recursively, despite the name), expand the query on a rowset, combining the query that selects the root rowset and query, which defines the adjacent rows so far selected. The syntax is a bit clumsy, but it is general and powerful.
RCTEs are supported in PostgreSQL, Firebird, SQL Server, and apparently in DB2. Oracle has a different but equivalent construct; I read that the latest versions support the correct RCTE. MySQL does not support RCTE. If you're not tied to MySQL, I would strongly recommend that you use PostgreSQL, which basically is a much better database.
However, it seems you do not need to support general graphics, just trees. In this case, more specific options are available to you.
One of them is a classic, but rather a thinking nested set .
The simplest way is to save the path with each line: it is a line that represents the position of the line in the tree and has the property that the path for the node is the path prefix for any subnode, which allows very efficient execution of various requests for origin ("is node A a child of node B? "," What is node A and node B the lowest common ancestor? ", Etc.). For example, you can build a path for a string by traversing a tree from the root and appending the identifiers of the strings encountered in the path with a slash. It's easy to build, but takes care to save if you change the tree. Using the path column, you can restrict the query to this tree simply by adding and path like '23/%' , where 23 is the root identifier.
So, although a graph database is probably the best way to store and query graph data, this is not the only option, and I would suggest that you weigh the benefits of using one of the benefits of having all your data in one database.
Tom Anderson Aug 08 2018-12-12T00: 00Z
source share