diff --git a/crates/cold-sql/migrations/002_add_topic_indexes.sql b/crates/cold-sql/migrations/002_add_topic_indexes.sql new file mode 100644 index 0000000..0843a5c --- /dev/null +++ b/crates/cold-sql/migrations/002_add_topic_indexes.sql @@ -0,0 +1,9 @@ +-- Add indexes on topic1, topic2, topic3 for log filtering. +-- +-- topic0 already has an index (001_initial). Most ERC-20/721 +-- Transfer lookups filter on topic1 (from) or topic2 (to), which +-- previously required sequential scans. + +CREATE INDEX IF NOT EXISTS idx_logs_topic1 ON logs (topic1); +CREATE INDEX IF NOT EXISTS idx_logs_topic2 ON logs (topic2); +CREATE INDEX IF NOT EXISTS idx_logs_topic3 ON logs (topic3); diff --git a/crates/cold-sql/src/backend.rs b/crates/cold-sql/src/backend.rs index 3065cd9..9056da6 100644 --- a/crates/cold-sql/src/backend.rs +++ b/crates/cold-sql/src/backend.rs @@ -128,6 +128,9 @@ impl SqlColdBackend { // connection that subsequent queries will use. let is_postgres = backend == "PostgreSQL"; sqlx::raw_sql(migration).execute(&pool).await?; + sqlx::raw_sql(include_str!("../migrations/002_add_topic_indexes.sql")) + .execute(&pool) + .await?; Ok(Self { pool, is_postgres }) }