From 1ad0e8688d9eb340bee9093c22f9dad83167f95e Mon Sep 17 00:00:00 2001 From: evazion Date: Sun, 20 Mar 2022 17:54:36 -0500 Subject: [PATCH] posts: fix timeouts for searches using sequential navigation. Fix certain searches timing out when using sequential navigation (page=b1234). The problem was that the so-called "small search optimization" (AKA: force Postgres to use the tag index for small searches instead a sequential scan) wasn't triggering because the ORDER BY clause for sequential navigation was `posts.id desc`, and we were only checking for `posts.id DESC`. --- app/logical/pagination_extension.rb | 4 ++-- app/logical/post_query_builder.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/logical/pagination_extension.rb b/app/logical/pagination_extension.rb index b5d7b4903..adbee96a3 100644 --- a/app/logical/pagination_extension.rb +++ b/app/logical/pagination_extension.rb @@ -64,11 +64,11 @@ module PaginationExtension end def paginate_sequential_before(before_id, limit) - where("#{table_name}.id < ?", before_id).reorder("#{table_name}.id desc").limit(limit + 1) + where("#{table_name}.id < ?", before_id).reorder("#{table_name}.id DESC").limit(limit + 1) end def paginate_sequential_after(after_id, limit) - where("#{table_name}.id > ?", after_id).reorder("#{table_name}.id asc").limit(limit + 1) + where("#{table_name}.id > ?", after_id).reorder("#{table_name}.id ASC").limit(limit + 1) end def paginate_numbered(page, limit) diff --git a/app/logical/post_query_builder.rb b/app/logical/post_query_builder.rb index 45c13dee7..63e74f02d 100644 --- a/app/logical/post_query_builder.rb +++ b/app/logical/post_query_builder.rb @@ -542,7 +542,7 @@ class PostQueryBuilder # better. def optimize_search(relation, small_search_threshold) return relation unless small_search_threshold.present? - return relation unless relation.order_values == ["posts.id DESC"] + return relation unless relation.order_values.map(&:downcase).in?([["posts.id desc"], ["posts.id asc"]]) if post_count.nil? # If post_count is nil, then the search took too long to count and we don't