-
-
Save sergii/948fc5eaca0f1e423f079d56a1cd56c9 to your computer and use it in GitHub Desktop.
Refs https://news.ycombinator.com/item?id=34222374 and https://ananthakumaran.in/2023/01/01/solving_n_plus_1_queries_on_rails.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE posts ( | |
id INTEGER PRIMARY KEY, | |
title TEXT NOT NULL, | |
content TEXT NOT NULL | |
); | |
CREATE TABLE comments ( | |
id INTEGER PRIMARY KEY, | |
content TEXT NOT NULL, | |
post_id INTEGER NOT NULL, | |
FOREIGN KEY (post_id) REFERENCES posts(id) | |
); | |
CREATE TABLE votes ( | |
id INTEGER PRIMARY KEY, | |
comment_id INTEGER NOT NULL, | |
FOREIGN KEY (comment_id) REFERENCES comments(id) | |
); | |
INSERT INTO posts (id, title, content) VALUES | |
(1, 'First post', 'This is the first post'), | |
(2, 'Second post', 'This is the second post'), | |
(3, 'Third post', 'This is the third post'); | |
INSERT INTO comments (id, content, post_id) VALUES | |
(1, 'First comment on first post', 1), | |
(2, 'Second comment on first post', 1), | |
(3, 'First comment on second post', 2), | |
(4, 'Second comment on second post', 2), | |
(5, 'First comment on third post', 3), | |
(6, 'Second comment on third post', 3); | |
INSERT INTO votes (id, comment_id) VALUES | |
(1, 1), (2, 1), (3, 1), | |
(4, 2), (5, 2), | |
(6, 3), (7, 3), (8, 3), (9, 3), | |
(10, 4), (11, 4), (12, 4), (13, 4), (14, 4), | |
(15, 5); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment