Category tree database design

I use Model List Adjacency to create categories, and it works great.

When searching for articles in a specific category (for example, electronics), I would also like to get articles in subcategories (for example, electronics → cameras or even electronics → cameras → lenses for cameras).

The way I do it now, pulls out all the identifiers of the category of electronic subcategories from the database and finds all the articles with category_id in this list.

This seems very inefficient and time consuming to me, as it can lead to a lot of queries to retrieve these subcategories.

Another way that I thought about this is that each article is associated with a tree of the entire category (for example, the article on camera lenses will also be associated with the categories of camera and electronics in the MANY_MANY table), and when I get all the articles in electronics it will also appear.

This would add a lot of redundant data to the database, although I might have to store 3 or 4 categories for each article. In addition, this will complicate actions such as moving an article to another category.

Is this the right way? Or is there a better / simpler way that I haven't thought about?

Any help appreciated!

+5
source share
1 answer

: MySQL.

, SELECT. , "" , , .

+12

All Articles