From 6f0f217379673a35f592d8ecaedf4cece85fb05c Mon Sep 17 00:00:00 2001 From: sneak Date: Sun, 27 Jul 2025 22:39:31 +0200 Subject: [PATCH] Add additional database indexes for better query performance - Add covering index for SELECT id queries (avoids table lookups) - Add stats index for COUNT queries on live_routes - Add index on asns.number for ASN lookups - Add index on bgp_peers.peer_ip for peer lookups These indexes should further reduce query times, especially: - The covering index includes the id column, eliminating the need to access the table after index lookup - The stats index helps with COUNT(*) queries - The asns.number index speeds up ASN lookups in transactions --- internal/database/schema.sql | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/internal/database/schema.sql b/internal/database/schema.sql index af3cc51..a3625cc 100644 --- a/internal/database/schema.sql +++ b/internal/database/schema.sql @@ -94,9 +94,23 @@ CREATE INDEX IF NOT EXISTS idx_live_routes_withdraw ON live_routes(prefix_id, peer_asn) WHERE withdrawn_at IS NULL; +-- Covering index for SELECT id queries (includes id in index) +CREATE INDEX IF NOT EXISTS idx_live_routes_covering + ON live_routes(prefix_id, origin_asn_id, peer_asn, id) + WHERE withdrawn_at IS NULL; + +-- Index for stats queries +CREATE INDEX IF NOT EXISTS idx_live_routes_stats + ON live_routes(withdrawn_at) + WHERE withdrawn_at IS NULL; + -- Additional indexes for prefixes table CREATE INDEX IF NOT EXISTS idx_prefixes_prefix ON prefixes(prefix); +-- Indexes for asns table +CREATE INDEX IF NOT EXISTS idx_asns_number ON asns(number); + -- Indexes for bgp_peers table CREATE INDEX IF NOT EXISTS idx_bgp_peers_asn ON bgp_peers(peer_asn); -CREATE INDEX IF NOT EXISTS idx_bgp_peers_last_seen ON bgp_peers(last_seen); \ No newline at end of file +CREATE INDEX IF NOT EXISTS idx_bgp_peers_last_seen ON bgp_peers(last_seen); +CREATE INDEX IF NOT EXISTS idx_bgp_peers_ip ON bgp_peers(peer_ip); \ No newline at end of file