From 06ec5f3e6bb66025a3ec1e5b4d314c7ff1e116c7 Mon Sep 17 00:00:00 2001 From: perekopskiy <53865202+perekopskiy@users.noreply.github.com> Date: Tue, 11 Jun 2024 10:55:11 +0300 Subject: [PATCH] fix(db): Optimize `get_l2_blocks_to_execute_for_l1_batch` (#2199) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## What ❔ Optimize `get_l2_blocks_to_execute_for_l1_batch` ## Why ❔ `transactions.l1_batch_number` is not indexed ## Checklist - [ ] PR title corresponds to the body of PR (we generate changelog entries from PRs). - [ ] Tests for the changes have been added / updated. - [ ] Documentation comments have been added / updated. - [ ] Code has been formatted via `zk fmt` and `zk lint`. - [ ] Spellcheck has been run via `zk spellcheck`. --- ...fb7a33a8fea8ab7fdefb7d9210673245a2a6f6c.json} | 4 ++-- core/lib/dal/src/transactions_dal.rs | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) rename core/lib/dal/.sqlx/{query-f63586d59264eab7388ad1de823227ecaa45d76d1ba260074898fe57c059a15a.json => query-f023e5fa599b279acd6ac02dffb7a33a8fea8ab7fdefb7d9210673245a2a6f6c.json} (86%) diff --git a/core/lib/dal/.sqlx/query-f63586d59264eab7388ad1de823227ecaa45d76d1ba260074898fe57c059a15a.json b/core/lib/dal/.sqlx/query-f023e5fa599b279acd6ac02dffb7a33a8fea8ab7fdefb7d9210673245a2a6f6c.json similarity index 86% rename from core/lib/dal/.sqlx/query-f63586d59264eab7388ad1de823227ecaa45d76d1ba260074898fe57c059a15a.json rename to core/lib/dal/.sqlx/query-f023e5fa599b279acd6ac02dffb7a33a8fea8ab7fdefb7d9210673245a2a6f6c.json index d62e213ef57b..2cd001b274da 100644 --- a/core/lib/dal/.sqlx/query-f63586d59264eab7388ad1de823227ecaa45d76d1ba260074898fe57c059a15a.json +++ b/core/lib/dal/.sqlx/query-f023e5fa599b279acd6ac02dffb7a33a8fea8ab7fdefb7d9210673245a2a6f6c.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "\n SELECT\n *\n FROM\n transactions\n WHERE\n l1_batch_number = $1\n ORDER BY\n miniblock_number,\n index_in_block\n ", + "query": "\n SELECT\n *\n FROM\n transactions\n WHERE\n miniblock_number BETWEEN (\n SELECT\n MIN(number)\n FROM\n miniblocks\n WHERE\n miniblocks.l1_batch_number = $1\n ) AND (\n SELECT\n MAX(number)\n FROM\n miniblocks\n WHERE\n miniblocks.l1_batch_number = $1\n )\n ORDER BY\n miniblock_number,\n index_in_block\n ", "describe": { "columns": [ { @@ -228,5 +228,5 @@ true ] }, - "hash": "f63586d59264eab7388ad1de823227ecaa45d76d1ba260074898fe57c059a15a" + "hash": "f023e5fa599b279acd6ac02dffb7a33a8fea8ab7fdefb7d9210673245a2a6f6c" } diff --git a/core/lib/dal/src/transactions_dal.rs b/core/lib/dal/src/transactions_dal.rs index fec3fe04946e..f76b61ec1646 100644 --- a/core/lib/dal/src/transactions_dal.rs +++ b/core/lib/dal/src/transactions_dal.rs @@ -1927,7 +1927,21 @@ impl TransactionsDal<'_, '_> { FROM transactions WHERE - l1_batch_number = $1 + miniblock_number BETWEEN ( + SELECT + MIN(number) + FROM + miniblocks + WHERE + miniblocks.l1_batch_number = $1 + ) AND ( + SELECT + MAX(number) + FROM + miniblocks + WHERE + miniblocks.l1_batch_number = $1 + ) ORDER BY miniblock_number, index_in_block