From 3cb22ecd3bccf93d9dcf5caa9d0773cd585e5568 Mon Sep 17 00:00:00 2001 From: hMB2 <0400eb0da32afbbf16cdd1dee6ffd243587e8f3d6a2823fe7dfa95d16b002b7d@buzz.block.builderlab.xyz> Date: Wed, 7 Oct 2026 01:55:34 -0700 Subject: [PATCH] db: drop idx_thread_metadata_root, a prefix of idx_thread_metadata_window With both indexes the planner can read a thread's latest reply through the narrower one and sort every reply. On a 20k-reply thread that cost 28ms of the sidebar; through the window index it is one probe (0.76ms). Signed-off-by: hMB2 <0400eb0da32afbbf16cdd1dee6ffd243587e8f3d6a2823fe7dfa95d16b002b7d@buzz.block.builderlab.xyz> --- migrations/0056_personal_read_state.sql | 5 +++++ schema/schema.sql | 1 - 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/migrations/0056_personal_read_state.sql b/migrations/0056_personal_read_state.sql index 4ea1b7e881..2d51ae8cf0 100644 --- a/migrations/0056_personal_read_state.sql +++ b/migrations/0056_personal_read_state.sql @@ -60,5 +60,10 @@ ALTER TABLE event_mentions ADD COLUMN received_at TIMESTAMPTZ, ADD COLUMN root_i CREATE INDEX idx_event_mentions_scope_received ON event_mentions (community_id, pubkey_hex, channel_id, root_id, received_at); +-- idx_thread_metadata_window (0049) leads with the same columns. With both, +-- the planner can take this narrower index for a thread's latest reply and +-- then sort every reply in the thread instead of reading the first row. +DROP INDEX IF EXISTS idx_thread_metadata_root; + SELECT attach_community_write_fence('personal_read_accounts'); SELECT attach_community_write_fence('personal_read_frontiers'); diff --git a/schema/schema.sql b/schema/schema.sql index 4c4378b66e..aaff2018df 100644 --- a/schema/schema.sql +++ b/schema/schema.sql @@ -576,7 +576,6 @@ CREATE TABLE thread_metadata ( ); CREATE INDEX idx_thread_metadata_parent ON thread_metadata (community_id, parent_event_id); -CREATE INDEX idx_thread_metadata_root ON thread_metadata (community_id, root_event_id); CREATE INDEX idx_thread_metadata_window ON thread_metadata (community_id, root_event_id, event_created_at DESC, event_id ASC); CREATE INDEX idx_thread_metadata_channel_depth