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开发者_如何学JAVA 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!
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.
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).
First one. You'll realize that when you have to "Count all the topics with these tags but not with these other".
The second method breaks the 1st rule of database normalization.
精彩评论