Skip to content

Commit

Permalink
fix: improve performance of assets__* by changing PKs
Browse files Browse the repository at this point in the history
* fix: improve performance of assets__* by changing PKs

* docs: add explanations to readme
  • Loading branch information
WuTao18 committed Apr 17, 2023
1 parent e25077b commit d522a98
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ And apply migrations
$ diesel migration run
```
If you have the DB with some data collected, and you need to apply the next migration, we highly recommend to read the migration contents.
Some migrations have the explanations what should be done, e.g. [[1]](migrations/2021-08-06-123500_account_changes_ordering_column/up.sql), [[2]](migrations/2023-02-02-100000_fungible_token_events_pk_changed/up.sql), [[3]](migrations/2023-02-02-110000_non_fungible_token_events_pk_changed/up.sql).
General advice is to add [`CONCURRENTLY` option](https://www.postgresql.org/docs/current/sql-createindex.html#SQL-CREATEINDEX-CONCURRENTLY) to all indexes creation and apply such changes manually.
### Compile NEAR Indexer for Explorer
```bash
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-- These commands are heavy for the full DB, consider adding CONCURRENTLY
CREATE UNIQUE INDEX assets__fungible_idx_tmp
ON assets__fungible_token_events (emitted_for_receipt_id,
emitted_at_block_timestamp,
emitted_in_shard_id,
emitted_index_of_event_entry_in_shard,
emitted_by_contract_account_id,
amount,
event_kind,
token_old_owner_account_id,
token_new_owner_account_id,
event_memo);
CREATE UNIQUE INDEX assets__fungible_token_events_unique
ON assets__fungible_token_events (emitted_for_receipt_id, emitted_index_of_event_entry_in_shard);

-- Next block runs ~1 sec even on the full DB
-- If you apply this manually, uncomment BEGIN TRANSACTION and COMMIT

-- BEGIN TRANSACTION;
SAVEPOINT change_ft_pks_back;
ALTER TABLE assets__fungible_token_events
DROP CONSTRAINT assets__fungible_token_events_pkey;
-- This command will automatically rename assets__fungible_idx_tmp to assets__fungible_token_events_pkey
ALTER TABLE assets__fungible_token_events
ADD CONSTRAINT assets__fungible_token_events_pkey PRIMARY KEY USING INDEX assets__fungible_idx_tmp;
RELEASE SAVEPOINT change_ft_pks_back;
-- COMMIT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- This command is heavy for the full DB, consider adding CONCURRENTLY
CREATE UNIQUE INDEX assets__fungible_idx_tmp
ON assets__fungible_token_events (emitted_for_receipt_id, emitted_index_of_event_entry_in_shard);

-- Next block runs ~1 sec even on the full DB
-- If you apply this manually, uncomment BEGIN TRANSACTION and COMMIT

-- BEGIN TRANSACTION;
SAVEPOINT change_ft_pks;
ALTER TABLE assets__fungible_token_events
DROP CONSTRAINT assets__fungible_token_events_pkey;
ALTER TABLE assets__fungible_token_events
DROP CONSTRAINT assets__fungible_token_events_unique;
-- This command will automatically rename assets__fungible_idx_tmp to assets__fungible_token_events_pkey
ALTER TABLE assets__fungible_token_events
ADD CONSTRAINT assets__fungible_token_events_pkey PRIMARY KEY USING INDEX assets__fungible_idx_tmp;
RELEASE SAVEPOINT change_ft_pks;
-- COMMIT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-- These commands are heavy for the full DB, consider adding CONCURRENTLY
CREATE UNIQUE INDEX assets__non_fungible_idx_tmp
ON assets__non_fungible_token_events (emitted_for_receipt_id,
emitted_at_block_timestamp,
emitted_in_shard_id,
emitted_index_of_event_entry_in_shard,
emitted_by_contract_account_id,
token_id,
event_kind,
token_old_owner_account_id,
token_new_owner_account_id,
token_authorized_account_id,
event_memo);
CREATE UNIQUE INDEX assets__non_fungible_token_events_unique
ON assets__non_fungible_token_events (emitted_for_receipt_id, emitted_index_of_event_entry_in_shard);

-- Next block runs ~1 sec even on the full DB
-- If you apply this manually, uncomment BEGIN TRANSACTION and COMMIT

-- BEGIN TRANSACTION;
SAVEPOINT change_nft_pks_back;
ALTER TABLE assets__non_fungible_token_events
DROP CONSTRAINT assets__non_fungible_token_events_pkey;
-- This command will automatically rename assets__non_fungible_idx_tmp to assets__non_fungible_token_events_pkey
ALTER TABLE assets__non_fungible_token_events
ADD CONSTRAINT assets__non_fungible_token_events_pkey PRIMARY KEY USING INDEX assets__non_fungible_idx_tmp;
RELEASE SAVEPOINT change_nft_pks_back;
-- COMMIT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- This command is heavy for the full DB, consider adding CONCURRENTLY
CREATE UNIQUE INDEX assets__non_fungible_idx_tmp
ON assets__non_fungible_token_events (emitted_for_receipt_id, emitted_index_of_event_entry_in_shard);

-- Next block runs ~1 sec even on the full DB
-- If you apply this manually, uncomment BEGIN TRANSACTION and COMMIT

-- BEGIN TRANSACTION;
SAVEPOINT change_nft_pks;
ALTER TABLE assets__non_fungible_token_events
DROP CONSTRAINT assets__non_fungible_token_events_pkey;
ALTER TABLE assets__non_fungible_token_events
DROP CONSTRAINT assets__non_fungible_token_events_unique;
-- This command will automatically rename assets__non_fungible_idx_tmp to assets__non_fungible_token_events_pkey
ALTER TABLE assets__non_fungible_token_events
ADD CONSTRAINT assets__non_fungible_token_events_pkey PRIMARY KEY USING INDEX assets__non_fungible_idx_tmp;
RELEASE SAVEPOINT change_nft_pks;
-- COMMIT;

0 comments on commit d522a98

Please sign in to comment.