Walking trees with SQL
Adjacency lists, nested sets/path enums, and OQGRAPH — pick by access pattern.
Adjacency list
CREATE TABLE taxon (
id INT PRIMARY KEY,
parent_id INT NULL,
name VARCHAR(100),
KEY (parent_id)
);
Cheap writes. Deep “all descendants” queries hurt without recursion helpers.
Nested sets / path enumeration
Fast subtree reads. Expensive writes. Choose when reads dominate.
OQGRAPH
Keep edges in a backing table; query algorithms in SQL (docs):
SELECT GROUP_CONCAT(linkid ORDER BY seq) AS lineage
FROM oq_graph
WHERE latch='dijkstras' AND origid=:root AND destid=:leaf;
Choose
| Pattern | Reads | Writes |
|---|---|---|
| Adjacency | slower deep walks | cheap |
| Nested sets / paths | fast subtrees | expensive |
| OQGRAPH | path/reachability | normal edge DML |
Match the traversal you run — not a universal tree religion.