I have a table of posts, a table of hashtags, and a table linking posts to hashtags, like so:
CREATE TABLE posts(
id SERIAL PRIMARY KEY,
post_data VARCHAR(128) NOT NULL
);
CREATE TABLE hashtags (
id SERIAL PRIMARY KEY,
value VARCHAR(128) NOT NULL
);
CREATE TABLE post_hashtags(
id SERIAL PRIMARY KEY,
post_id INTEGER NOT NULL REFERENCES posts(id),
hashtag_id INTEGER NOT NULL REFERENCES hashtag(id)
);
INSERT INTO posts(post_data) VALUES ('post1');
INSERT INTO hashtags (value) VALUES ('hashtag1'), ('hashtag2'), ('hashtag3');
-- At this point I might want to add links between post1 and existing hashtags as well as possibly new ones
Whenever a user makes a post using several hash tags, I want to:
- Create a new row in
postsand get the ID - Create new rows in
hashtagsfor hashtags that don't exist and get their IDs - For each hashtag, create a row in
post_hashtagswith the post and hashtag ids
Right now, I'm capable of handling it on the server side but that's terrible performance, obviously: insert post and get ID; for each hashtag, insert into hashtags if it doesn't exist, get the ID; then insert into the post_hashtags. Lots of calls to DB that I'm guessing could be simplified, but my SQL skills are lacking at the moment.
idcolumn inpost_hashtagsisn't necessary; however, there should be a unique constraint on(post_id, hashtag_id)to preclude redundant rows.