Skip to content

Commit d5b9f4d

Browse files
committed
add indexes for new tables
1 parent d07e2b2 commit d5b9f4d

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

database/database_migrations/migrate_contracts.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ migrate_partition() {
1818
LAG(block_height) OVER (PARTITION BY account_id ORDER BY block_height DESC) AS block_height_to
1919
FROM state_changes_contract_$partition
2020
)
21-
INSERT INTO state_changes_contract_compact$partition (account_id, data_value, block_height_from, block_height_to)
21+
INSERT INTO state_changes_contract_compact_$partition (account_id, data_value, block_height_from, block_height_to)
2222
SELECT
2323
account_id,
2424
data_value,
@@ -41,4 +41,4 @@ for i in $(seq 0 99); do
4141
done
4242

4343
# Wait for all background jobs to finish
44-
wait
44+
wait

database/src/postgres/migrations/shard_db/20250218132509_create_state_changes_compact.up.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ DECLARE
1414
BEGIN
1515
FOR i IN 0..99 LOOP
1616
EXECUTE format('CREATE TABLE IF NOT EXISTS state_changes_data_compact_%s PARTITION OF state_changes_data_compact FOR VALUES WITH (MODULUS 100, REMAINDER %s)', i, i);
17+
-- Indexes for state_changes_data_compact partitions
18+
EXECUTE format('CREATE INDEX IF NOT EXISTS idx_data_%s_keys ON state_changes_data_compact_%s (account_id, data_key) WHERE block_height_to IS NULL;', i, i);
1719
END LOOP;
1820
END $$;
1921

@@ -34,6 +36,8 @@ DECLARE
3436
BEGIN
3537
FOR i IN 0..99 LOOP
3638
EXECUTE format('CREATE TABLE IF NOT EXISTS state_changes_access_key_compact_%s PARTITION OF state_changes_access_key_compact FOR VALUES WITH (MODULUS 100, REMAINDER %s)', i, i);
39+
-- Indexes for state_changes_access_key_compact partitions
40+
EXECUTE format('CREATE INDEX IF NOT EXISTS idx_access_key_%s_keys ON state_changes_access_key_compact_%s (account_id, data_key) WHERE block_height_to IS NULL;', i, i);
3741
END LOOP;
3842
END $$;
3943

@@ -53,6 +57,8 @@ DECLARE
5357
BEGIN
5458
FOR i IN 0..99 LOOP
5559
EXECUTE format('CREATE TABLE IF NOT EXISTS state_changes_contract_compact_%s PARTITION OF state_changes_contract_compact FOR VALUES WITH (MODULUS 100, REMAINDER %s)', i, i);
60+
-- Indexes for state_changes_contract_compact partitions
61+
EXECUTE format('CREATE INDEX IF NOT EXISTS idx_contract_%s_acc ON state_changes_contract_compact_%s (account_id) WHERE block_height_to IS NULL;', i, i);
5662
END LOOP;
5763
END $$;
5864

@@ -71,5 +77,7 @@ DECLARE
7177
BEGIN
7278
FOR i IN 0..99 LOOP
7379
EXECUTE format('CREATE TABLE IF NOT EXISTS state_changes_account_compact_%s PARTITION OF state_changes_account_compact FOR VALUES WITH (MODULUS 100, REMAINDER %s)', i, i);
80+
-- Indexes for state_changes_account_compact partitions
81+
EXECUTE format('CREATE INDEX IF NOT EXISTS idx_account_%s_acc ON state_changes_account_compact_%s (account_id) WHERE block_height_to IS NULL;', i, i);
7482
END LOOP;
7583
END $$;

0 commit comments

Comments
 (0)