Database-design for a small filter / search tag function

All we need is an easy explanation of the problem, so here it is.

I’m building a search function in php for a database with 1k to 4k articles. It’s going to be built on a tag system. I’ve been reading some tutorials, blog posts, q&a etc about it.

Most important for me is performance, I want it to be fast and light. In my special case I’m only going to have a set of few fixed tags. Example: Color:blue, Size:large etc. So if user filters/search “Large Blue” only the articles with these tags should show. There’s going to be “Ajaxed” queries, so a light and fast query is important.

I can think of three ways of doing this:

Option 1:
Articles table
– id
– name
– tags (with a data string of tags comma separated)
– etc

Option 2.
Articles table
– id
– name
– etc

Tags table
– id
– name
– article ids (with a data string of article ids comma separated)

Option 3.
Articles table
– id
– name
– etc

Relational Tag Arts table
– Fkey tags id
– Fkey article id

Tags table
– id
– name
– etc

Which one of these would be fastest / best performance. I know it’s not good practice to use comma separated data strings in a table field. But for me, since I just have a few set of tags, option 1 or 2 looks simple and faster than option 3?

Any other suggestions is of course welcome! Thanks

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

In my opinion option 3 is the best.

Option 1 has the problem that you always have to use a like on the tags field to find the articles.

Option 2 will cause a rewrite of the tag each time that an article is added or deleted. When there are a lot of articles for the tag then the record becomes long.

Both option 1 and 2 will end up with ‘big’ rows which means less rows in a physical block which means more physical reads and ‘slower’ result.

Option 3 has a simple setup. It is the way to present an M-to-N relation. Put an index on the name of the tag to help to speed up the finding of the tags (will not help much if you have a very limited number of tags). Adding an article or adding a tag to an article will be fast too.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply