mirror of
https://github.com/basecamp/once-campfire.git
synced 2026-10-09 00:00:12 +09:00
Find the messages a refresh replaces through an index
Every room visit and every reconnection asks for the messages updated since the page was rendered. That query filtered the room by updated_at and sorted it by created_at, so SQLite walked every message in the room: about 220-360 ms in a room of a million messages. An index on (room_id, updated_at) finds the few updated messages directly. Sorting on +created_at keeps SQLite on that index once messages are also indexed by (room_id, created_at): with both, the planner otherwise walks the room in creation order looking for updated rows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JVFo3Lt9T8M5NR7KxVvsZ2
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
require "test_helper"
|
||||
|
||||
class Message::PaginationTest < ActiveSupport::TestCase
|
||||
test "messages updated since a time are found through the room and update time index" do
|
||||
assert_match(/USING INDEX index_messages_on_room_id_and_updated_at \(room_id=\? AND updated_at>\?\)/, plan_of_page_updated_since)
|
||||
end
|
||||
|
||||
test "messages updated since a time are the newest page of them, in creation order" do
|
||||
room = rooms(:watercooler)
|
||||
first, second, third = room.messages.ordered.first(3)
|
||||
|
||||
travel 1.minute do
|
||||
[ third, first, second ].each(&:touch)
|
||||
|
||||
stub_const(Message::Pagination, :PAGE_SIZE, 2) do
|
||||
assert_equal [ second, third ], room.messages.page_updated_since(30.seconds.ago).to_a
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def plan_of_page_updated_since
|
||||
statements = []
|
||||
callback = ->(*, payload) { statements << payload[:sql] if payload[:sql].start_with?(%(SELECT "messages")) }
|
||||
ActiveSupport::Notifications.subscribed(callback, "sql.active_record") do
|
||||
rooms(:watercooler).messages.page_updated_since(1.minute.ago)
|
||||
end
|
||||
|
||||
Message.connection.select_rows("EXPLAIN QUERY PLAN #{statements.sole}").map(&:last).join(" | ")
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user