Skip to content

Commit e422918

Browse files
rswansonclaude
andauthored
feat(cold-sql): add topic indexes for log filtering (#54)
* feat(cold-sql): add topic1/topic2/topic3 indexes for log filtering Only topic0 was indexed previously. Most ERC-20/721 Transfer event lookups filter on topic1 (from address) or topic2 (to address), causing sequential scans on the logs table. Adds migration 002 with indexes on all three remaining topic columns. Uses CREATE INDEX IF NOT EXISTS for idempotent application. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * refactor: consolidate duplicate topic index migration The 002 migration SQL is dialect-agnostic (CREATE INDEX IF NOT EXISTS), so a single file suffices for both PostgreSQL and SQLite. Removes the duplicate _pg variant and uses a shared include_str! outside the backend match arm. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent c90dbff commit e422918

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- Add indexes on topic1, topic2, topic3 for log filtering.
2+
--
3+
-- topic0 already has an index (001_initial). Most ERC-20/721
4+
-- Transfer lookups filter on topic1 (from) or topic2 (to), which
5+
-- previously required sequential scans.
6+
7+
CREATE INDEX IF NOT EXISTS idx_logs_topic1 ON logs (topic1);
8+
CREATE INDEX IF NOT EXISTS idx_logs_topic2 ON logs (topic2);
9+
CREATE INDEX IF NOT EXISTS idx_logs_topic3 ON logs (topic3);

crates/cold-sql/src/backend.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ impl SqlColdBackend {
127127
// Execute via pool to ensure the migration uses the same
128128
// connection that subsequent queries will use.
129129
sqlx::raw_sql(migration).execute(&pool).await?;
130+
sqlx::raw_sql(include_str!("../migrations/002_add_topic_indexes.sql"))
131+
.execute(&pool)
132+
.await?;
130133
Ok(Self { pool, is_postgres })
131134
}
132135

0 commit comments

Comments
 (0)