From e65c90c062245d3b582da11cc37fb399df6e9eb8 Mon Sep 17 00:00:00 2001 From: Greg B Date: Fri, 29 Nov 2024 11:51:47 +1100 Subject: [PATCH] Calculate minimum transaction id in scope of after-block-height restriction in a slightly more efficient way --- files/grest/rpc/account/account_txs.sql | 18 ++++++++++++++---- files/grest/rpc/address/address_txs.sql | 17 +++++++++++++---- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/files/grest/rpc/account/account_txs.sql b/files/grest/rpc/account/account_txs.sql index 5f92628c..2721c9cb 100644 --- a/files/grest/rpc/account/account_txs.sql +++ b/files/grest/rpc/account/account_txs.sql @@ -12,10 +12,20 @@ DECLARE _tx_id_list bigint[]; _stake_address_id integer; BEGIN - SELECT INTO _tx_id_min id - FROM tx - WHERE block_id >= (SELECT id FROM block WHERE block_no >= _after_block_height ORDER BY id limit 1) - ORDER BY id limit 1; + + SELECT INTO _tx_id_min min(id) + FROM tx + WHERE block_id = ( + SELECT id + FROM block AS b + WHERE block_no >= _after_block_height + AND EXISTS ( + SELECT true + FROM tx t + WHERE t.block_id = b.id + ) + ORDER BY id LIMIT 1 + ); SELECT INTO _stake_address_id id FROM stake_address WHERE hash_raw = (SELECT DECODE(b32_decode(_stake_address), 'hex')); diff --git a/files/grest/rpc/address/address_txs.sql b/files/grest/rpc/address/address_txs.sql index 6efb4f5d..6e8e9bcc 100644 --- a/files/grest/rpc/address/address_txs.sql +++ b/files/grest/rpc/address/address_txs.sql @@ -11,10 +11,19 @@ DECLARE _tx_id_min bigint; _tx_id_list bigint[]; BEGIN - SELECT INTO _tx_id_min id - FROM tx - WHERE block_id >= (SELECT id FROM block WHERE block_no >= _after_block_height ORDER BY id limit 1) - ORDER BY id limit 1; + SELECT INTO _tx_id_min min(id) + FROM tx + WHERE block_id = ( + SELECT id + FROM block AS b + WHERE block_no >= _after_block_height + AND EXISTS ( + SELECT true + FROM tx t + WHERE t.block_id = b.id + ) + ORDER BY id LIMIT 1 + ); -- all tx_out & tx_in tx ids SELECT INTO _tx_id_list ARRAY_AGG(tx_id)