|
| 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 | +}; |
0 commit comments