Closed frol closed 3 years ago
@frol Do I need to think only about this request, or I need to check the speed of all queries and optimize them all? The header sound scarier than the problem inside 🙂
Apart of additional index, I can suggest add WHERE clause:
SELECT
transactions.transaction_hash as hash,
transactions.signer_account_id as signer_id,
transactions.receiver_account_id as receiver_id,
transactions.included_in_block_hash as block_hash,
DIV(transactions.block_timestamp, 1000*1000) as block_timestamp,
transactions.index_in_chunk as transaction_index
FROM transactions
where transactions.block_timestamp > 1627561319124288426
-- (select transactions.block_timestamp - 600000000000 as aa from transactions order by transactions.block_timestamp desc limit 1)
-- (select (extract(epoch from now()) - 600) * 1000000000)
ORDER BY transactions.block_timestamp DESC, transactions.index_in_chunk DESC
LIMIT 10;
We can use real-world data: we know that we will surely have 10 transactions in 10 minutes. It allows us to cut the bunch of data dramatically. The query above takes ~50ms.
The only detail is that we should pass the timestamp parameter: I tried to use subselects, with
clause, anything: it always degrade the performance dramatically. But it looks like it's not the problem to pass it in JS code.
@frol what do you think?
UPD we can pass it this way, right from SQL
SELECT
transactions.transaction_hash as hash,
transactions.signer_account_id as signer_id,
transactions.receiver_account_id as receiver_id,
transactions.included_in_block_hash as block_hash,
DIV(transactions.block_timestamp, 1000*1000) as block_timestamp,
transactions.index_in_chunk as transaction_index
FROM transactions
where transactions.block_timestamp > ((round(extract(epoch from now())) - 600) * 1000000000)::numeric
ORDER BY transactions.block_timestamp DESC, transactions.index_in_chunk DESC
LIMIT 10;
@telezhnaya Good point, but we use this query for all sorts of tasks (e.g. extending with WHERE signer_account_id = '...'
), where this trick won't work.
Let's try adding the index (use the CREATE INDEXER CONCURRENTLY
to avoid blocking the DB), and see how long it will take, and how big is the impact, and whether it will help with the query optimization.
Here is the query from Explorer:
It is slow as hell, and the list of transactions page on Explorer takes a while to display, and soon it will be completely unusable.
Currently, due to the fact that we order by a combination of columns, the execution plan picks a super slow path of sequential scan:
If I remove the
index_in_chunk
, the query is super fast:I can see two options:
block_timestamp
andindex_in_chunk