Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database design of a tree-like category system

I'm using the Adjacency List Model to create categories, and it works perfectly.

When retrieving articles in a certain category (for example electronics), I would like to also retrieve the articles in the sub categories (for example electronics->cameras, or even electronics->cameras->camera lenses).

The way I am doing it now is pulling from the DB all the category id's of the sub categories of electronics, and finding all articles with a category_id in this list.

This seems to me very inefficient and time-consuming, since this could result in many queries to retrieve these sub categories.

Another way I thought of doing this is having every article associated with the whole category tree (for example an article about camera lenses will also be associated with the camera and electronics categories in MANY_MANY table), and when I retrieve all articles in electronics it will also appear.

This would add a lot of redundant data to the database though, as I might have to store 3 or 4 categories for each article. Also, it would complicate actions like moving an article to another category.

Is this the right way to go? Or is there a better/simpler way that I have not thought of?

Any help appreciated!

like image 655
EdanB Avatar asked Jan 04 '10 07:01

EdanB


People also ask

Which database is best for tree structure?

Database type Unless you're aiming at huge amounts of data I suggest you go with any of the SQL databases that you know best (MSSQL, MySQL, Oracle). But if your database will contain enormous number of hierarchy nodes then flirting with a specialised graph-oriented database may be a better option.

How do you represent a tree in database?

You could use adjacency lists (your current idea), nested sets, or even (with appropriate database support) arrays of node ids to represent the path from the root. Which representation you choose depends on what you need to do to your data. @Kim, why array? If the graph is a tree, each node has at most one parent.

What is the database design?

Database design is the organization of data according to a database model. The designer determines what data must be stored and how the data elements interrelate. With this information, they can begin to fit the data to the database model. Database management system manages the data accordingly.

What is a good database design?

A good database design is, therefore, one that: Divides your information into subject-based tables to reduce redundant data. Provides Access with the information it requires to join the information in the tables together as needed. Helps support and ensure the accuracy and integrity of your information.


1 Answers

Have a read of this article about Nested Set Modelling: Managing Hierarchical Data in MySQL.

Using the suggested technique, you can get entire trees or subtrees in one single SELECT. It's a bit more complicated than the "normal" approach, but it's totally worth it if you're going to be doing lots of reads from the table.

like image 50
nickf Avatar answered Sep 29 '22 11:09

nickf