diff --git a/.changelog/890.feature.md b/.changelog/890.feature.md new file mode 100644 index 000000000..b46dcb306 --- /dev/null +++ b/.changelog/890.feature.md @@ -0,0 +1 @@ +storage/consensus: Update indexes to better serve the related accounts queries diff --git a/storage/migrations/00_consensus.up.sql b/storage/migrations/00_consensus.up.sql index 49d2c6334..18bfc61c8 100644 --- a/storage/migrations/00_consensus.up.sql +++ b/storage/migrations/00_consensus.up.sql @@ -81,7 +81,9 @@ CREATE TABLE chain.transactions CREATE INDEX ix_transactions_sender_block ON chain.transactions (sender, block); CREATE INDEX ix_transactions_tx_hash ON chain.transactions (tx_hash); --`method` is a possible external API parameter; `block` lets us efficiently retrieve the most recent N txs with a given method. -CREATE INDEX ix_transactions_method_height ON chain.transactions (method, block); +CREATE INDEX ix_transactions_method_height ON chain.transactions (method, block); -- Removed in 12_related_transactions_method_idx.up.sql. +-- Added in 12_related_transactions_method_idx.up.sql. +CREATE INDEX ix_transactions_method_block_tx_index ON chain.transactions (method, block DESC, tx_index); CREATE TABLE chain.events ( @@ -346,7 +348,9 @@ CREATE TABLE chain.accounts_related_transactions FOREIGN KEY (tx_block, tx_index) REFERENCES chain.transactions(block, tx_index) DEFERRABLE INITIALLY DEFERRED ); CREATE INDEX ix_accounts_related_transactions_block ON chain.accounts_related_transactions (tx_block); -CREATE INDEX ix_accounts_related_transactions_address_block ON chain.accounts_related_transactions(account_address, tx_block); +CREATE INDEX ix_accounts_related_transactions_address_block ON chain.accounts_related_transactions(account_address, tx_block); -- Removed in 12_related_transactions_method_idx.up.sql. +-- Added in 12_related_transactions_method_idx.up.sql. +-- CREATE INDEX ix_accounts_related_transactions_address_block_desc_tx_index ON chain.accounts_related_transactions (account_address, tx_block DESC, tx_index); -- Tracks the current (consensus) height of the node. CREATE TABLE chain.latest_node_heights diff --git a/storage/migrations/12_related_transactions_method_idx.up.sql b/storage/migrations/12_related_transactions_method_idx.up.sql new file mode 100644 index 000000000..2660346b8 --- /dev/null +++ b/storage/migrations/12_related_transactions_method_idx.up.sql @@ -0,0 +1,9 @@ +BEGIN; + +CREATE INDEX IF NOT EXISTS ix_transactions_method_block_tx_index ON chain.transactions (method, block DESC, tx_index); +DROP INDEX IF EXISTS chain.ix_transactions_method_height; + +CREATE INDEX IF NOT EXISTS ix_accounts_related_transactions_address_block_desc_tx_index ON chain.accounts_related_transactions (account_address, tx_block DESC, tx_index); +DROP INDEX IF EXISTS chain.ix_accounts_related_transactions_address_block; + +COMMIT;