mirror of
https://github.com/alexgo-io/stacks-blockchain-api.git
synced 2026-01-12 22:43:34 +08:00
fix: rosetta account/balance speed
* fix: optimize stx_events query * fix: add missing indexes
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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' },
|
||||
|
||||
@@ -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)?
|
||||
|
||||
@@ -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');
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user