Closure table equivalent for graph structures in SQL Closure table equivalent for graph structures in SQL database database

Closure table equivalent for graph structures in SQL


I did the presentation you linked to, and I've been asked about implementing general graphs with a similar method, but I've never gotten around to it.

Certainly there are problems with the technique if you have cyclic graphs, unless you can unambiguously identify a "starting node." Because otherwise if you start with any node in a cycle, you'd want to be able to traverse the whole cycle in the graph.

It might be easier in SQL using a recursive CTE, but I most often use MySQL which doesn't support CTE syntax until version 8.0. And if you do have recursive CTE capability, you'd be better off using that instead of a closure table, because you have less chance for data anomalies.

Another option is to explore a specialized graph database. For MySQL/MariaDB, there's a community storage engine that optimizes for tree and graph queries: https://openquery.com.au/products/graph-engine