views:

154

answers:

4

I want to save tags related to a discussion post. The same as the 'tags' that Stack Overflow uses. What is the best way to do this?

I was thinking of saving them in the 'discussion' table as a single comma separated string, but I'm not sure if this is the 'fastest' way assuming there will be lots and lots of rows in the discussion table.

+5  A: 

In general you shouldn't store tags as a single comma separated field. You would not be able to define an index on the separate tags. In this case, a query for a tag would require a full table scan, which will be slow, and will become slower as you add more rows to the table.

You may be interested in checking out earlier Stack Overflow questions, which discuss this topic further:

Daniel Vassallo
Thanks for these resources. I'll check them out.
tappit
+5  A: 

Storing tags in a single field is definitely a poor design decision since you won't be able to index the field. Depending on the expected size of the tables, a simple relational design such as the one below could be all you need.

Tags:

id | tag
------------
 1 | mysql
 2 | postgres
 3 | mssql

Discussions:

id | something | something else
-------------------------------
 1 | abcde     | asdfg
 2 | fghij     | hkijk
 3 | klmno     | lmnop

Tags_2_discussions:

id | tag | discussion
----------------------
 1 | 1   | 1
 2 | 2   | 4
 3 | 2   | 4
code_burgar
Is this the "Toxi" solution mentioned in this link:http://www.pui.ch/phred/archives/2005/04/tags-database-schemas.html
tappit
@tappit: Pretty much.
code_burgar
A: 

I use a very simple structure.

Basically, you have a tag table with the tag name and an RID in it, then a tag_link that has a tag_rid in it and a owner_post that is a foreign key of your discussion post table.

I find it works well... though its bad that you must join 2 tables to get the list of tags..

Earlz
A: 

How about thinking out of the RDBMS box and applying a Semantic Web/RDF based approach?

You could use something like 4Store with the SPARQL frontend and simple triplets (turtle notation) for the tags.

Dieter
That sounds so crazy it might just work... Or it could fireball and die horribly....
ck