Models for hierarchical data (2010)
slideshare.net
slideshare.net
See https://maxdemarzi.com/2016/10/27/catalogs-and-hierarchies/ for a quick example. Read until the end 'cause the kicker is being able to go UP a hierarchy in no time at all.
See http://www.slideshare.net/StampedeCon/managing-genetic-ances... for a real world use case with Neo4j vs Oracle RAC comparisons.
I did a bunch of research on this while working on a comments system. You pay a price in table space if your hierarchies tend to get very deep. But for most applications I can think of, it's the way to go, which you'll see if you page through to Slide 69.
http://seespotcode.net/2016/04/30/static-trees/
It's got some pretty serious practical limitations, but I found it an interesting exploration.
Yeah, O(n^2) could hurt. I guess you have to have some idea of what average and max depths are to figure out how many rows you're talking about. But the other advantages are pretty compelling.
That is why such models are sometimes called "naive trees" - it is the obvious way to fit the minimum requirements (and, of course, sometimes all the requirements) but some types of reporting on the structure are inefficient in the general case (or impossible without advanced query features like CTEs).
> subtrees can be generated in the database layer by recursive CTEs
Be careful with CTEs in postgres: they are an optimisation fence with regard to predicate pushing so for some query patterns you'll see index scans where you think it might be able to use seeks.
This may be the case in other DBs too. It isn't in SQL Server (usually), I don't know about others.
I've had some good success with materialized path solutions in Postgres since its support for arrays makes them indexable and easy to query.
I'd suggest picking whichever is easiest to maintain, and works well with the shape and structure of your data. If you use Rails and Postgres I have a few articles including this one that might help illustrate how it can all play together with an ORM: http://www.monkeyandcrow.com/blog/hierarchies_with_rails/