tags:

views:

42

answers:

4

For each ad, I allow users to choose up to 5 tags. Right now, in my database, I have it like...

Posting_id     TagID
    5            1
    5            2 
    5            3
    6            5
    6            1

But i was thinking if I should make it like...

    Posting_id     TagID
    5              1 2 3 
    6              5 1

Then first option is much easier to insert and retrieve data. But if I have 100 posts with 3 tags each, that's 300 rows...so ALOT more rows

The second option requires using explode() impode(), etc but it is much cleaner.

Which option should I do and why? thanks!

EDIT: The first way is better!

+3  A: 

Use the first option because the second option makes it difficult to index the TagID column effectively. If you ever want to query your data on for a specific numeric value stored in the TagID column (and I'm betting you will), you'll get poor performance out of the second option.

Asaph
good thinking, I'm keeping it the same.
ggfan
+2  A: 

Do it the first way. Rows are cheap, and mysqsl can handle tables with millions and millions of rows. Also, doing it the first way keeps he ID as an integer, which can make future work with the table much easier (e.g., when doing joins and referring to foreign keys).

eykanal
yeah I failed to see that. And counting the number of tags I have for a tag would be a lot easier the first way.
ggfan
A: 

First one. You'll realize that when you have to "Count all the topics with these tags but not with these other".

Ben
yeah that just hit me. thanks!
ggfan
A: 

The second method breaks the 1st rule of database normalization.

robert mcbean