MySQL Database Design – Small Filter/Search Tag Function

database-designMySQLperformancePHP

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

Best Answer

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.