Skip to content

Commit

Permalink
storage/consensus: Update indexes to better serve the related account…
Browse files Browse the repository at this point in the history
…s queries
  • Loading branch information
ptrus committed Jan 19, 2025
1 parent 886dbe6 commit fba9b10
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions .changelog/890.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
storage/consensus: Update indexes to better serve the related accounts queries
8 changes: 6 additions & 2 deletions storage/migrations/00_consensus.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
(
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions storage/migrations/12_related_transactions_method_idx.up.sql
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit fba9b10

Please sign in to comment.