Skip to content

Commit b18d140

Browse files
committed
feat: add index to optimize update_effective_volumes trigger
Add idx_moves_update_effective_volumes index to prevent sequential scans on every INSERT when MOVES_HISTORY feature is enabled (ON by default). The update_effective_volumes trigger runs after each move insertion and updates all moves with effective_date > new.effective_date. Without this index, this operation causes significant performance degradation at scale. Changes: - New migration 45: Create idx_moves_update_effective_volumes - Migration 45 renamed to 46: Updated to drop moves_range_dates
1 parent 1f493e9 commit b18d140

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Add Index for Trigger Optimization (3/5)
2+
description: |
3+
Create optimized index for the update_effective_volumes trigger that runs on every
4+
INSERT when MOVES_HISTORY feature is enabled (ON by default).
5+
6+
This trigger updates all moves where effective_date > new.effective_date, which
7+
requires an efficient index on (accounts_address, asset, effective_date) to avoid
8+
full table scans on large datasets.
9+
10+
changes:
11+
moves_table:
12+
- action: "CREATE"
13+
new_index: "idx_moves_update_effective_volumes (accounts_address, asset, effective_date)"
14+
reason: |
15+
Optimizes the UPDATE query in update_effective_volumes trigger.
16+
Without this index, every INSERT causes a slow sequential scan when
17+
MOVES_HISTORY is ON, causing severe performance degradation.
18+
19+
The existing moves_range_dates index has the same columns but in a
20+
different order (account_address, asset, effective_date) and was created
21+
before the column rename. This new index ensures optimal performance.
22+
23+
affected_queries:
24+
- "update_effective_volumes trigger: UPDATE moves WHERE effective_date > ?"
25+
- "Runs on EVERY INSERT when MOVES_HISTORY=ON (default configuration)"
26+
27+
performance_impact:
28+
- "CRITICAL improvement for write performance with MOVES_HISTORY enabled"
29+
- "Reduces INSERT latency by eliminating sequential scans on moves table"
30+
- "Essential for maintaining performance at scale with history tracking"
31+
32+
migration_safety:
33+
- "Index creation is non-blocking"
34+
- "No data modification"
35+
- "moves_range_dates will be dropped in next migration (46) as redundant"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
set search_path = '{{.Schema}}';
2+
3+
-- ========================================
4+
-- MOVES TABLE INDEX FOR TRIGGER OPTIMIZATION (3/5)
5+
-- ========================================
6+
7+
-- Critical: Index to optimize update_effective_volumes trigger
8+
-- This trigger runs on EVERY INSERT when MOVES_HISTORY feature is ON
9+
-- The trigger updates all moves with effective_date > new.effective_date
10+
-- Replaces/optimizes: moves_range_dates which has suboptimal column order
11+
create index idx_moves_update_effective_volumes
12+
on "{{.Schema}}".moves (accounts_address, asset, effective_date);

internal/storage/bucket/migrations/45-drop-old-indexes/notes.yaml renamed to internal/storage/bucket/migrations/46-drop-old-indexes/notes.yaml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
name: Drop Old Indexes (5/5)
1+
name: Drop Old Indexes (6/6)
22
description: |
33
Drop all old suboptimal indexes that have been replaced by new optimized indexes
4-
created in migrations 41-44. This final step completes the index optimization process.
4+
created in migrations 41-45. This final step completes the index optimization process.
55
66
changes:
77
moves_table:
88
- action: "DROP"
99
old_indexes:
1010
- "moves_post_commit_volumes"
1111
- "moves_effective_post_commit_volumes"
12-
reason: "Replaced by idx_moves_pit_insertion and idx_moves_pit_effective in migrations 41-42"
12+
- "moves_range_dates"
13+
reason: |
14+
- moves_post_commit_volumes and moves_effective_post_commit_volumes
15+
replaced by idx_moves_pit_insertion and idx_moves_pit_effective in migrations 41-42
16+
- moves_range_dates replaced by idx_moves_update_effective_volumes in migration 45
1317
1418
accounts_metadata_table:
1519
- action: "DROP"

internal/storage/bucket/migrations/45-drop-old-indexes/up.sql renamed to internal/storage/bucket/migrations/46-drop-old-indexes/up.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ set search_path = '{{.Schema}}';
44
-- DROP OLD INDEXES
55
-- ========================================
66

7-
-- Drop old moves table indexes (replaced by migrations 41-42)
7+
-- Drop old moves table indexes (replaced by migrations 41-42-45)
88
drop index if exists moves_post_commit_volumes;
99
drop index if exists moves_effective_post_commit_volumes;
10+
drop index if exists moves_range_dates;
1011

1112
-- Drop old accounts_metadata index (replaced by migration 43)
1213
drop index if exists accounts_metadata_revisions;

0 commit comments

Comments
 (0)