I have a table of users, a table of hashtags, and a table linking the users to hashtags, like so:
Table users {
id,
name
}
Table hashtags {
id,
value
}
Table user_hashtags {
id,
user_id,
hashtag_id
}
Whenever a user makes a post using several hash tags, I want to:
- Create a new row in
users
if needed and get the ID - Create new rows in
hashtags
for hashtags that don’t exist and get their IDs - For each hashtag, create a row in
user_hashtags
with the user and hashtag ids
Right now I’m capable of handling it on the server side but obviously that’s terrible performance; for each hashtag, insert into hashtags
if it doesn’t exist and get the ID; then insert into the user_hashtags
. Lots of calls to DB that I’m guessing could be simplified, but my SQL skills are lacking at the moment. I’m not even quite sure how t