fix: rosetta account/balance speed

* fix: optimize stx_events query

* fix: add missing indexes
This commit is contained in:
Rafael Cárdenas
2021-10-19 09:09:46 -05:00
committed by GitHub
parent 26e50fd1b0
commit c49a4d4a1d
4 changed files with 11 additions and 10 deletions

View File

@@ -4370,18 +4370,15 @@ export class PgDataStore
debit_total: string | null;
}>(
`
WITH transfers AS (
SELECT amount, sender, recipient
FROM stx_events
WHERE canonical = true AND microblock_canonical = true AND (sender = $1 OR recipient = $1) AND block_height <= $2
), credit AS (
WITH credit AS (
SELECT sum(amount) as credit_total
FROM transfers
WHERE recipient = $1
), debit AS (
FROM stx_events
WHERE canonical = true AND microblock_canonical = true AND recipient = $1 AND block_height <= $2
),
debit AS (
SELECT sum(amount) as debit_total
FROM transfers
WHERE sender = $1
FROM stx_events
WHERE canonical = true AND microblock_canonical = true AND sender = $1 AND block_height <= $2
)
SELECT credit_total, debit_total
FROM credit CROSS JOIN debit

View File

@@ -172,6 +172,7 @@ export async function up(pgm: MigrationBuilder): Promise<void> {
pgm.createIndex('txs', 'canonical');
pgm.createIndex('txs', ['canonical', 'microblock_canonical']);
pgm.createIndex('txs', ['sender_address', 'block_height']);
pgm.createIndex('txs', [
{ name: 'block_height', sort: 'DESC' },

View File

@@ -71,6 +71,8 @@ export async function up(pgm: MigrationBuilder): Promise<void> {
pgm.createIndex('stx_events', 'event_index');
pgm.createIndex('stx_events', ['canonical', 'microblock_canonical']);
pgm.createIndex('stx_events', ['sender', 'block_height']);
pgm.createIndex('stx_events', ['recipient', 'block_height']);
// TODO(mb): this and other tx metadata rows could probably only use a composite index on (parent_index_block_hash, microblock_hash)?
// also maybe only a composite index on (canonical, microblock_canonical)?

View File

@@ -67,5 +67,6 @@ export async function up(pgm: MigrationBuilder): Promise<void> {
pgm.createIndex('stx_lock_events', 'microblock_canonical');
pgm.createIndex('stx_lock_events', 'locked_address');
pgm.createIndex('stx_lock_events', 'event_index');
pgm.createIndex('stx_lock_events', 'unlock_height');
}