]> Untitled Git - lemmy.git/commitdiff
Fixing hot_ranks and scores to append a published sort. (#3618)
authorDessalines <dessalines@users.noreply.github.com>
Thu, 20 Jul 2023 14:44:23 +0000 (10:44 -0400)
committerGitHub <noreply@github.com>
Thu, 20 Jul 2023 14:44:23 +0000 (16:44 +0200)
- #3428

crates/db_views/src/comment_view.rs
crates/db_views/src/post_view.rs
migrations/2023-07-14-154840_add_optimized_indexes_published/down.sql [new file with mode: 0644]
migrations/2023-07-14-154840_add_optimized_indexes_published/up.sql [new file with mode: 0644]

index 889adeeb73ed301e2727f12d543d3032f187d563..e98d9f879996222b3a842b1272a10fce387fd5d1 100644 (file)
@@ -360,10 +360,14 @@ impl<'a> CommentQuery<'a> {
     };
 
     query = match self.sort.unwrap_or(CommentSortType::Hot) {
-      CommentSortType::Hot => query.then_order_by(comment_aggregates::hot_rank.desc()),
+      CommentSortType::Hot => query
+        .then_order_by(comment_aggregates::hot_rank.desc())
+        .then_order_by(comment_aggregates::published.desc()),
+      CommentSortType::Top => query
+        .order_by(comment_aggregates::score.desc())
+        .then_order_by(comment_aggregates::published.desc()),
       CommentSortType::New => query.then_order_by(comment::published.desc()),
       CommentSortType::Old => query.then_order_by(comment::published.asc()),
-      CommentSortType::Top => query.order_by(comment_aggregates::score.desc()),
     };
 
     // Note: deleted and removed comments are done on the front side
index 30693afb69bfbc424d102582073aabb7fc2560f9..1d85df3c83e0ea6cd51817d7f378556fd528915c 100644 (file)
@@ -416,8 +416,12 @@ impl<'a> PostQuery<'a> {
     }
 
     query = match self.sort.unwrap_or(SortType::Hot) {
-      SortType::Active => query.then_order_by(post_aggregates::hot_rank_active.desc()),
-      SortType::Hot => query.then_order_by(post_aggregates::hot_rank.desc()),
+      SortType::Active => query
+        .then_order_by(post_aggregates::hot_rank_active.desc())
+        .then_order_by(post_aggregates::published.desc()),
+      SortType::Hot => query
+        .then_order_by(post_aggregates::hot_rank.desc())
+        .then_order_by(post_aggregates::published.desc()),
       SortType::New => query.then_order_by(post_aggregates::published.desc()),
       SortType::Old => query.then_order_by(post_aggregates::published.asc()),
       SortType::NewComments => query.then_order_by(post_aggregates::newest_comment_time.desc()),
diff --git a/migrations/2023-07-14-154840_add_optimized_indexes_published/down.sql b/migrations/2023-07-14-154840_add_optimized_indexes_published/down.sql
new file mode 100644 (file)
index 0000000..5661a31
--- /dev/null
@@ -0,0 +1,26 @@
+-- Drop the new indexes
+drop index idx_post_aggregates_featured_local_most_comments;
+drop index idx_post_aggregates_featured_local_hot;
+drop index idx_post_aggregates_featured_local_active;
+drop index idx_post_aggregates_featured_local_score;
+drop index idx_post_aggregates_featured_community_hot;
+drop index idx_post_aggregates_featured_community_active;
+drop index idx_post_aggregates_featured_community_score;
+drop index idx_post_aggregates_featured_community_most_comments;
+drop index idx_comment_aggregates_hot;
+drop index idx_comment_aggregates_score;
+
+-- Add the old ones back in
+-- featured_local
+create index idx_post_aggregates_featured_local_hot on post_aggregates (featured_local desc, hot_rank desc);
+create index idx_post_aggregates_featured_local_active on post_aggregates (featured_local desc, hot_rank_active desc);
+create index idx_post_aggregates_featured_local_score on post_aggregates (featured_local desc, score desc);
+
+-- featured_community
+create index idx_post_aggregates_featured_community_hot on post_aggregates (featured_community desc, hot_rank desc);
+create index idx_post_aggregates_featured_community_active on post_aggregates (featured_community desc, hot_rank_active desc);
+create index idx_post_aggregates_featured_community_score on post_aggregates (featured_community desc, score desc);
+
+create index idx_comment_aggregates_hot on comment_aggregates (hot_rank desc);
+create index idx_comment_aggregates_score on comment_aggregates (score desc);
+
diff --git a/migrations/2023-07-14-154840_add_optimized_indexes_published/up.sql b/migrations/2023-07-14-154840_add_optimized_indexes_published/up.sql
new file mode 100644 (file)
index 0000000..94e426f
--- /dev/null
@@ -0,0 +1,30 @@
+-- Drop the old indexes
+drop index idx_post_aggregates_featured_local_hot;
+drop index idx_post_aggregates_featured_local_active;
+drop index idx_post_aggregates_featured_local_score;
+drop index idx_post_aggregates_featured_community_hot;
+drop index idx_post_aggregates_featured_community_active;
+drop index idx_post_aggregates_featured_community_score;
+drop index idx_comment_aggregates_hot;
+drop index idx_comment_aggregates_score;
+
+-- Add a published desc, to the end of the hot and active ranks
+
+-- Add missing most comments index
+create index idx_post_aggregates_featured_local_most_comments on post_aggregates (featured_local desc, comments desc, published desc);
+create index idx_post_aggregates_featured_community_most_comments on post_aggregates (featured_community desc, comments desc, published desc);
+
+-- featured_local
+create index idx_post_aggregates_featured_local_hot on post_aggregates (featured_local desc, hot_rank desc, published desc);
+create index idx_post_aggregates_featured_local_active on post_aggregates (featured_local desc, hot_rank_active desc, published desc);
+create index idx_post_aggregates_featured_local_score on post_aggregates (featured_local desc, score desc, published desc);
+
+-- featured_community
+create index idx_post_aggregates_featured_community_hot on post_aggregates (featured_community desc, hot_rank desc, published desc);
+create index idx_post_aggregates_featured_community_active on post_aggregates (featured_community desc, hot_rank_active desc, published desc);
+create index idx_post_aggregates_featured_community_score on post_aggregates (featured_community desc, score desc, published desc);
+
+-- Fixing some comment aggregates ones
+create index idx_comment_aggregates_hot on comment_aggregates (hot_rank desc, published desc);
+create index idx_comment_aggregates_score on comment_aggregates (score desc, published desc);
+