Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hierarchical Data Models: Adjacency List vs. Nested Sets

I have a product catalog. Each category consists of different number (in deep) of subcategories. The number of levels (deep) is unknown, but I quite sure that it will not be exceed of 5,6 levels. The data changes are much more rarely then reads.

The question is: what type of hierarchical data model is more suitable for such situation. The project is based on Django framework and it's peculiarities (admin i-face, models handling...) should be considered.

Many thanks!

like image 527
S2201 Avatar asked May 27 '09 12:05

S2201


People also ask

Which data model is good in adjacency?

The Adjacency List Model is both the most common and the worst possible tree model. On the other hand, it is the best way to model any general graph.

What is a nested data model?

A nested data model is simply a class that inherits from INestedDataModel . You can add any properties you like and annotate them in the same way you would with any other data model.


1 Answers

The Adjacency List is much easier to maintain and Nested Sets are a lot faster to query.

The problem has always been that converting an Adjacency List to Nested Sets has taken way too long thanks to a really nasty "push stack" method that's loaded with RBAR. So people end up doing some really difficult maintenance in Nested Sets or not using them.

Now, you can have your cake and eat it, too! You can do the conversion on 100,000 nodes in less than 4 seconds and on a million rows in less than a minute! All in T-SQL, by the way! Please see the following articles.

Hierarchies on Steroids #1: Convert an Adjacency List to Nested Sets

Hierarchies on Steroids #2: A Replacement for Nested Sets Calculations

like image 87
Jeff Moden Avatar answered Oct 02 '22 12:10

Jeff Moden