-
-
Notifications
You must be signed in to change notification settings - Fork 857
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Denormalize community_id into post_aggregates for a 1000x speed-up wh…
…en loading posts (#3653) * Denormalize community_id into post_aggregates for a 1000x speed-up when loading posts * Remove unused index * Add creator_id to post_aggregates * Use post_aggregates as main table for PostQuery * Make post_aggregates the main table for PostView * Reformat SQL
- Loading branch information
Showing
6 changed files
with
89 additions
and
24 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
20 changes: 20 additions & 0 deletions
20
migrations/2023-07-18-082614_post_aggregates_community_id/down.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
-- This file should undo anything in `up.sql` | ||
|
||
CREATE OR REPLACE FUNCTION post_aggregates_post() | ||
RETURNS trigger | ||
LANGUAGE plpgsql | ||
AS | ||
$$ | ||
BEGIN | ||
IF (TG_OP = 'INSERT') THEN | ||
INSERT INTO post_aggregates (post_id, published, newest_comment_time, newest_comment_time_necro) | ||
VALUES (NEW.id, NEW.published, NEW.published, NEW.published); | ||
ELSIF (TG_OP = 'DELETE') THEN | ||
DELETE FROM post_aggregates WHERE post_id = OLD.id; | ||
END IF; | ||
RETURN NULL; | ||
END | ||
$$; | ||
|
||
ALTER TABLE post_aggregates DROP COLUMN community_id, DROP COLUMN creator_id; | ||
|
35 changes: 35 additions & 0 deletions
35
migrations/2023-07-18-082614_post_aggregates_community_id/up.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
-- Your SQL goes here | ||
ALTER TABLE post_aggregates | ||
ADD COLUMN community_id integer REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE, | ||
ADD COLUMN creator_id integer REFERENCES person ON UPDATE CASCADE ON DELETE CASCADE; | ||
|
||
CREATE OR REPLACE FUNCTION post_aggregates_post() | ||
RETURNS trigger | ||
LANGUAGE plpgsql | ||
AS | ||
$$ | ||
BEGIN | ||
IF (TG_OP = 'INSERT') THEN | ||
INSERT INTO post_aggregates (post_id, | ||
published, | ||
newest_comment_time, | ||
newest_comment_time_necro, | ||
community_id, | ||
creator_id) | ||
VALUES (NEW.id, NEW.published, NEW.published, NEW.published, NEW.community_id, NEW.creator_id); | ||
ELSIF (TG_OP = 'DELETE') THEN | ||
DELETE FROM post_aggregates WHERE post_id = OLD.id; | ||
END IF; | ||
RETURN NULL; | ||
END | ||
$$; | ||
|
||
UPDATE post_aggregates | ||
SET community_id=post.community_id, | ||
creator_id=post.creator_id | ||
FROM post | ||
WHERE post.id = post_aggregates.post_id; | ||
|
||
ALTER TABLE post_aggregates | ||
ALTER COLUMN community_id SET NOT NULL, | ||
ALTER COLUMN creator_id SET NOT NULL; |