database design question here.
Say we had a webapp or something that uses hashtags for 20-40 word notes. What is the best way to store a user's hashtags.
For instance, if a user entered. "I like to have #lunch at #sizzler"
we would store the sentence as text and we could store the hashtags as JSON, a comma separated list or some other mechanism.
Its also worth pointing out that the tags need to be searchable, such as how many people have been hash tagging lunch, etc.
Advise on the matter would be great, I always get a bit stumped when it comes to storing variable sized inputs in mysql. There can be an infinite number of hashtags per note, what is the best way to store them?
A hashtag is a type of tag used to describe topics on social networking websites, most notably Twitter. Hashtags, like all tags, are a type of metadata (data about data).
I'd suggest using intermediary third table for storing tags<=>items associations, since we have many-to-many relations between tags and items, i.e. one item can be associated with multiple tags and one tag can be associated with multiple items.
I would advise going with a typical many-to-many-relationship between messages and tags.
That would mean you need 3 tables.
Messages
(columns Id
, UserId
and Content
)Tags
(columns Id
and TagName
)TagMessageRelations
(columns: MessageId
and TagId
- to make the connections between messages and tags - via foreign keys pointing to Messages.Id
/ Tags.Id
)That way you do not store a tag multiple times but only create a new relation to a message (if that tag already exists in the tag-table of course).
This way you can
SELECT COUNT(*) FROM Tags
) SELECT COUNT(*) FROM Tags INNER JOIN TagMessageRelations ON Tags.Id = TagMessageRelations.TagId INNER JOIN Messages ON TagMessageRelations.MessageId = Messages.Id GROUP BY Messages.UserId
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With