ggfan
ggfan

Reputation: 2492

Which of the two ways should I use to insert tags into mysql?

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!

Upvotes: 1

Views: 97

Answers (4)

Robert McBean
Robert McBean

Reputation: 67

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

Upvotes: 0

Ben
Ben

Reputation: 16533

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

Upvotes: 0

eykanal
eykanal

Reputation: 27017

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).

Upvotes: 2

Asaph
Asaph

Reputation: 162771

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.

Upvotes: 3

Related Questions