Read the newest search matches off the index instead of sorting them all

Search showed the last 100 matches by created_at, so SQLite collected every
message containing the words and sorted them before keeping a page. A common
word in a large account meant sorting most of its history on every search.

Ordering by the full-text index's rowid, which is the message id, lets SQLite
walk the index from the newest match and stop once the page is full.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JVFo3Lt9T8M5NR7KxVvsZ2
This commit is contained in:
Marcello Costagliola
2026-10-05 02:50:48 +02:00
parent 254dd1d46f
commit edaab3e5d1
3 changed files with 24 additions and 1 deletions
+1 -1
View File
@@ -20,7 +20,7 @@ class SearchesController < ApplicationController
private
def set_messages
if query.present?
@messages = Current.user.reachable_messages.search(query).with_presentation.last_page_of(100)
@messages = Current.user.reachable_messages.search(query).with_presentation.last_page_of_matches(100)
else
@messages = Message.none
end
+8
View File
@@ -9,6 +9,14 @@ module Message::Searchable
scope :search, ->(query) { joins("join message_search_index idx on messages.id = idx.rowid").where("idx.body match ?", query).ordered }
end
class_methods do
# Orders by the index's rowid, which is the message id, so SQLite walks the full-text index
# newest first and stops at the page. Ordering by created_at sorted every match before paging.
def last_page_of_matches(size)
Message::Pagination::Page.load(reorder("idx.rowid"), :last, size)
end
end
private
def create_in_index
execute_sql_with_binds "insert into message_search_index(rowid, body) values (?, ?)", id, plain_text_body
+15
View File
@@ -20,6 +20,21 @@ class Message::SearchableTest < ActiveSupport::TestCase
assert_equal messages, rooms(:designers).messages.search("cat")
end
test "the last page of matches holds the newest ones, read off the index without sorting every match" do
messages = [ "first cat", "second cat", "third cat" ].map do |body|
rooms(:designers).messages.create! body: body, client_message_id: body, creator: users(:david)
end
queries = []
collect = ->(*, payload) { queries << payload[:sql] if payload[:sql].include?("message_search_index") }
page = ActiveSupport::Notifications.subscribed(collect, "sql.active_record") do
rooms(:designers).messages.search("cat").last_page_of_matches(2)
end
assert_equal messages.last(2), page
assert_no_match(/TEMP B-TREE/, Message.connection.select_rows("EXPLAIN QUERY PLAN #{queries.sole}").map(&:last).join(" | "))
end
test "rich text body is converted to plain text for indexing" do
message = rooms(:designers).messages.create! body: "<span>My hovercraft is full of eels</span>", client_message_id: "earth", creator: users(:david)