From e42ecb36e9335d3d8fce4c367b3e323e94913f81 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 +++++++++++++---- files/grest/rpc/address/credential_txs.sql | 17 +++++++++++++---- 3 files changed, 40 insertions(+), 12 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) diff --git a/files/grest/rpc/address/credential_txs.sql b/files/grest/rpc/address/credential_txs.sql index bc534e9a..5bbac123 100644 --- a/files/grest/rpc/address/credential_txs.sql +++ b/files/grest/rpc/address/credential_txs.sql @@ -19,10 +19,19 @@ BEGIN FROM UNNEST(_payment_credentials) AS cred_hex ) AS tmp; - 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)