Skip to content

Commit 9da4dcd

Browse files
authored
fix: indexes to optimize principal-based etag db lookups (#2157)
* fix: indexes to optimize principal-based etag db lookups * chore: fix mempool_txs index and query * fix: force usage of idx_nft_events_optimized index
1 parent e3c30c6 commit 9da4dcd

File tree

2 files changed

+81
-5
lines changed

2 files changed

+81
-5
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/** @param { import("node-pg-migrate").MigrationBuilder } pgm */
2+
exports.up = pgm => {
3+
// Indexes used to speed up queries in the `getPrincipalLastActivityTxIds` function:
4+
// https://github.com/hirosystems/stacks-blockchain-api/blob/e3c30c6e0cb14843d5f089b64010d738b0b27763/src/datastore/pg-store.ts#L4440-L4492
5+
// See issue https://github.com/hirosystems/stacks-blockchain-api/issues/2147
6+
7+
pgm.createIndex(
8+
'ft_events',
9+
[
10+
'sender',
11+
'recipient',
12+
{ name: 'block_height', order: 'DESC' },
13+
{ name: 'microblock_sequence', order: 'DESC' },
14+
{ name: 'tx_index', order: 'DESC' },
15+
{ name: 'event_index', order: 'DESC' }
16+
],
17+
{
18+
name: 'idx_ft_events_optimized',
19+
where: 'canonical = TRUE AND microblock_canonical = TRUE',
20+
}
21+
);
22+
23+
pgm.createIndex(
24+
'nft_events',
25+
[
26+
'sender',
27+
'recipient',
28+
{ name: 'block_height', order: 'DESC' },
29+
{ name: 'microblock_sequence', order: 'DESC' },
30+
{ name: 'tx_index', order: 'DESC' },
31+
{ name: 'event_index', order: 'DESC' }
32+
],
33+
{
34+
name: 'idx_nft_events_optimized',
35+
where: 'canonical = TRUE AND microblock_canonical = TRUE',
36+
}
37+
);
38+
39+
pgm.createIndex(
40+
'mempool_txs',
41+
[
42+
'sender_address',
43+
'sponsor_address',
44+
'token_transfer_recipient_address',
45+
{ name: 'receipt_time', order: 'DESC' }
46+
],
47+
{
48+
name: 'idx_mempool_txs_optimized',
49+
where: 'pruned = FALSE',
50+
}
51+
);
52+
};
53+
54+
/** @param { import("node-pg-migrate").MigrationBuilder } pgm */
55+
exports.down = pgm => {
56+
pgm.dropIndex('ft_events', ['sender', 'recipient', 'block_height', 'microblock_sequence', 'tx_index', 'event_index'], { name: 'idx_ft_events_optimized' });
57+
pgm.dropIndex('nft_events', ['sender', 'recipient', 'block_height', 'microblock_sequence', 'tx_index', 'event_index'], { name: 'idx_nft_events_optimized' });
58+
pgm.dropIndex('mempool_txs', ['sender_address', 'sponsor_address', 'token_transfer_recipient_address', 'receipt_time'], { name: 'idx_mempool_txs_optimized' });
59+
};

src/datastore/pg-store.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4453,10 +4453,27 @@ export class PgStore extends BasePgStore {
44534453
UNION
44544454
(
44554455
SELECT '0x' || encode(tx_id, 'hex') AS tx_id
4456-
FROM ft_events
4457-
WHERE (sender = ${principal} OR recipient = ${principal})
4458-
AND canonical = true
4459-
AND microblock_canonical = true
4456+
FROM (
4457+
(
4458+
SELECT tx_id, block_height, microblock_sequence, tx_index, event_index
4459+
FROM ft_events
4460+
WHERE sender = ${principal}
4461+
AND canonical = true
4462+
AND microblock_canonical = true
4463+
ORDER BY block_height DESC, microblock_sequence DESC, tx_index DESC, event_index DESC
4464+
LIMIT 1
4465+
)
4466+
UNION ALL
4467+
(
4468+
SELECT tx_id, block_height, microblock_sequence, tx_index, event_index
4469+
FROM ft_events
4470+
WHERE recipient = ${principal}
4471+
AND canonical = true
4472+
AND microblock_canonical = true
4473+
ORDER BY block_height DESC, microblock_sequence DESC, tx_index DESC, event_index DESC
4474+
LIMIT 1
4475+
)
4476+
) AS combined
44604477
ORDER BY block_height DESC, microblock_sequence DESC, tx_index DESC, event_index DESC
44614478
LIMIT 1
44624479
)
@@ -4480,7 +4497,7 @@ export class PgStore extends BasePgStore {
44804497
(sender_address = ${principal}
44814498
OR sponsor_address = ${principal}
44824499
OR token_transfer_recipient_address = ${principal})
4483-
ORDER BY receipt_time DESC, sender_address DESC, nonce DESC
4500+
ORDER BY receipt_time DESC
44844501
LIMIT 1
44854502
)`
44864503
: this.sql``

0 commit comments

Comments
 (0)