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
+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)