From 948d79dafbf94bcad481817d9fadb34a80475462 Mon Sep 17 00:00:00 2001 From: Bretton Date: Sat, 18 Oct 2025 01:27:26 +0000 Subject: [PATCH] fix(db): remove redundant index and add missing record_uri index Database optimization changes: 1. Removed redundant idx_blocks_user_community index: - UNIQUE constraint on (user_did, community_did) already creates an index automatically - Maintaining duplicate index wastes storage and degrades write performance (every insert updates two identical indexes) 2. Added missing idx_blocks_record_uri index: - Required for GetBlockByURI() queries used in Jetstream DELETE operations - Without this index, DELETE event processing does full table scan Migration now has optimal indexes without redundancy. Addresses: Critical review comments #1 and #7 --- internal/db/migrations/009_create_community_blocks_table.sql | 5 +++-- 1 file(s) changed, 3 insertion(s)(+), 2 deletion(s)(-) diff --git a/internal/db/migrations/009_create_community_blocks_table.sql b/internal/db/migrations/009_create_community_blocks_table.sql --- a/internal/db/migrations/009_create_community_blocks_table.sql +++ b/internal/db/migrations/009_create_community_blocks_table.sql @@ -14,14 +14,15 @@ UNIQUE(user_did, community_did) ); -- Indexes for efficient queries +-- Note: UNIQUE constraint on (user_did, community_did) already creates an index for those columns CREATE INDEX idx_blocks_user ON community_blocks(user_did); CREATE INDEX idx_blocks_community ON community_blocks(community_did); -CREATE INDEX idx_blocks_user_community ON community_blocks(user_did, community_did); +CREATE INDEX idx_blocks_record_uri ON community_blocks(record_uri); -- For GetBlockByURI (Jetstream DELETE operations) CREATE INDEX idx_blocks_blocked_at ON community_blocks(blocked_at); -- +goose Down DROP INDEX IF EXISTS idx_blocks_blocked_at; -DROP INDEX IF EXISTS idx_blocks_user_community; +DROP INDEX IF EXISTS idx_blocks_record_uri; DROP INDEX IF EXISTS idx_blocks_community; DROP INDEX IF EXISTS idx_blocks_user; DROP TABLE IF EXISTS community_blocks; -- tangled.sh