From 8a792508e938e6bf4fe2ee2eec9328570a7602a7 Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 12 Aug 2016 14:36:38 +1000 Subject: [PATCH] PERF: improve offset discovery query --- lib/search.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/search.rb b/lib/search.rb index 5d32f14692f..c09907716e4 100644 --- a/lib/search.rb +++ b/lib/search.rb @@ -131,10 +131,14 @@ class Search def self.min_post_id_no_cache return 0 unless SiteSetting.search_prefer_recent_posts? - offset = Post.unscoped.count - SiteSetting.search_recent_posts_size - return 0 if offset <= 0 - Post.unscoped.order(:id).offset(offset).limit(1).pluck(:id)[0] + offset, has_more = Post.unscoped + .order('id desc') + .offset(SiteSetting.search_recent_posts_size-1) + .limit(2) + .pluck(:id) + + has_more ? offset : 0 end def self.min_post_id(opts=nil)