From 47f4aa3550e7e79736f87673e007f930a9442f1f Mon Sep 17 00:00:00 2001 From: Dessalines Date: Sat, 18 Feb 2023 09:35:35 -0500 Subject: [PATCH] Fixing slow joins to post_read, post_saved, and comment_saved . (#2738) - Fixes #2445 --- crates/db_views/src/comment_view.rs | 4 ++-- crates/db_views/src/post_view.rs | 6 +++--- docker/docker-compose.yml | 3 ++- .../down.sql | 1 + .../2023-02-15-212546_add_post_comment_saved_indexes/up.sql | 2 ++ 5 files changed, 10 insertions(+), 6 deletions(-) create mode 100644 migrations/2023-02-15-212546_add_post_comment_saved_indexes/down.sql create mode 100644 migrations/2023-02-15-212546_add_post_comment_saved_indexes/up.sql diff --git a/crates/db_views/src/comment_view.rs b/crates/db_views/src/comment_view.rs index 1819c04d..be2e4cc8 100644 --- a/crates/db_views/src/comment_view.rs +++ b/crates/db_views/src/comment_view.rs @@ -311,7 +311,7 @@ impl<'a> CommentQuery<'a> { } if self.saved_only.unwrap_or(false) { - query = query.filter(comment_saved::id.is_not_null()); + query = query.filter(comment_saved::comment_id.is_not_null()); } if !self.show_deleted_and_removed.unwrap_or(true) { @@ -325,7 +325,7 @@ impl<'a> CommentQuery<'a> { if self.local_user.is_some() { // Filter out the rows with missing languages - query = query.filter(local_user_language::id.is_not_null()); + query = query.filter(local_user_language::language_id.is_not_null()); // Don't show blocked communities or persons query = query.filter(community_block::person_id.is_null()); diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index 0804815d..88ff904b 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -365,17 +365,17 @@ impl<'a> PostQuery<'a> { }; if self.saved_only.unwrap_or(false) { - query = query.filter(post_saved::id.is_not_null()); + query = query.filter(post_saved::post_id.is_not_null()); } // Only hide the read posts, if the saved_only is false. Otherwise ppl with the hide_read // setting wont be able to see saved posts. else if !self.local_user.map(|l| l.show_read_posts).unwrap_or(true) { - query = query.filter(post_read::id.is_null()); + query = query.filter(post_read::post_id.is_null()); } if self.local_user.is_some() { // Filter out the rows with missing languages - query = query.filter(local_user_language::id.is_not_null()); + query = query.filter(local_user_language::language_id.is_not_null()); // Don't show blocked communities or persons query = query.filter(community_block::person_id.is_null()); diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index f923c97d..7ed15150 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -99,7 +99,8 @@ services: "postgres", "-c", "session_preload_libraries=auto_explain", "-c", "auto_explain.log_min_duration=5ms", - "-c", "auto_explain.log_analyze=true" + "-c", "auto_explain.log_analyze=true", + "-c", "track_activity_query_size=1048576" ] networks: - lemmyinternal diff --git a/migrations/2023-02-15-212546_add_post_comment_saved_indexes/down.sql b/migrations/2023-02-15-212546_add_post_comment_saved_indexes/down.sql new file mode 100644 index 00000000..3ef1500c --- /dev/null +++ b/migrations/2023-02-15-212546_add_post_comment_saved_indexes/down.sql @@ -0,0 +1 @@ +drop index idx_post_saved_person_id, idx_comment_saved_person_id; diff --git a/migrations/2023-02-15-212546_add_post_comment_saved_indexes/up.sql b/migrations/2023-02-15-212546_add_post_comment_saved_indexes/up.sql new file mode 100644 index 00000000..88994312 --- /dev/null +++ b/migrations/2023-02-15-212546_add_post_comment_saved_indexes/up.sql @@ -0,0 +1,2 @@ +create index idx_post_saved_person_id on post_saved (person_id); +create index idx_comment_saved_person_id on comment_saved (person_id); -- 2.44.1