From 8427cddcbd5ba13388e5b96fb988128f8dabe0f4 Mon Sep 17 00:00:00 2001 From: Alex Ostrovski Date: Wed, 12 Jun 2024 12:31:41 +0300 Subject: [PATCH] revert(pruning): Revert pruning query (#2220) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## What ❔ Reverts matter-labs/zksync-era#2179 ## Why ❔ The new query turned out to be slower than the old one. --- ...6ba34fd131682ee5414a9d0ae2cab349b2395.json | 15 ++++ ...1cf4274a870c0ff7801e61807ff78cfe398f8.json | 16 ----- core/lib/dal/src/pruning_dal/mod.rs | 68 +++++++------------ 3 files changed, 38 insertions(+), 61 deletions(-) create mode 100644 core/lib/dal/.sqlx/query-362e20c4c2527f1585132ca85316ba34fd131682ee5414a9d0ae2cab349b2395.json delete mode 100644 core/lib/dal/.sqlx/query-6ebb549e274b7e684cde480c78e1cf4274a870c0ff7801e61807ff78cfe398f8.json diff --git a/core/lib/dal/.sqlx/query-362e20c4c2527f1585132ca85316ba34fd131682ee5414a9d0ae2cab349b2395.json b/core/lib/dal/.sqlx/query-362e20c4c2527f1585132ca85316ba34fd131682ee5414a9d0ae2cab349b2395.json new file mode 100644 index 000000000000..ef84a26a6e84 --- /dev/null +++ b/core/lib/dal/.sqlx/query-362e20c4c2527f1585132ca85316ba34fd131682ee5414a9d0ae2cab349b2395.json @@ -0,0 +1,15 @@ +{ + "db_name": "PostgreSQL", + "query": "\n DELETE FROM storage_logs\n WHERE\n storage_logs.miniblock_number < $1\n AND hashed_key IN (\n SELECT\n hashed_key\n FROM\n storage_logs\n WHERE\n miniblock_number BETWEEN $1 AND $2\n )\n ", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Int8", + "Int8" + ] + }, + "nullable": [] + }, + "hash": "362e20c4c2527f1585132ca85316ba34fd131682ee5414a9d0ae2cab349b2395" +} diff --git a/core/lib/dal/.sqlx/query-6ebb549e274b7e684cde480c78e1cf4274a870c0ff7801e61807ff78cfe398f8.json b/core/lib/dal/.sqlx/query-6ebb549e274b7e684cde480c78e1cf4274a870c0ff7801e61807ff78cfe398f8.json deleted file mode 100644 index fc65c45e323c..000000000000 --- a/core/lib/dal/.sqlx/query-6ebb549e274b7e684cde480c78e1cf4274a870c0ff7801e61807ff78cfe398f8.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "\n DELETE FROM storage_logs\n WHERE\n ctid IN (\n SELECT\n prev_logs.ctid\n FROM\n storage_logs AS prev_logs\n INNER JOIN LATERAL (\n SELECT\n 1\n FROM\n storage_logs AS current_logs\n WHERE\n current_logs.miniblock_number BETWEEN $1 AND $2\n AND current_logs.hashed_key = prev_logs.hashed_key\n ) AS current_logs ON TRUE\n WHERE\n prev_logs.miniblock_number < $1\n LIMIT\n $3\n )\n ", - "describe": { - "columns": [], - "parameters": { - "Left": [ - "Int8", - "Int8", - "Int8" - ] - }, - "nullable": [] - }, - "hash": "6ebb549e274b7e684cde480c78e1cf4274a870c0ff7801e61807ff78cfe398f8" -} diff --git a/core/lib/dal/src/pruning_dal/mod.rs b/core/lib/dal/src/pruning_dal/mod.rs index 702a301e7438..9a5356202aee 100644 --- a/core/lib/dal/src/pruning_dal/mod.rs +++ b/core/lib/dal/src/pruning_dal/mod.rs @@ -318,51 +318,29 @@ impl PruningDal<'_, '_> { &mut self, l2_blocks_to_prune: ops::RangeInclusive, ) -> DalResult { - /// Number of past logs to delete in a single query run. - const BATCHING_LIMIT: i64 = 10_000; - - let mut total_rows_affected = 0; - loop { - let execution_result = sqlx::query!( - r#" - DELETE FROM storage_logs - WHERE - ctid IN ( - SELECT - prev_logs.ctid - FROM - storage_logs AS prev_logs - INNER JOIN LATERAL ( - SELECT - 1 - FROM - storage_logs AS current_logs - WHERE - current_logs.miniblock_number BETWEEN $1 AND $2 - AND current_logs.hashed_key = prev_logs.hashed_key - ) AS current_logs ON TRUE - WHERE - prev_logs.miniblock_number < $1 - LIMIT - $3 - ) - "#, - i64::from(l2_blocks_to_prune.start().0), - i64::from(l2_blocks_to_prune.end().0), - BATCHING_LIMIT - ) - .instrument("hard_prune_batches_range#prune_storage_logs_from_past_l2_blocks") - .with_arg("l2_blocks_to_prune", &l2_blocks_to_prune) - .report_latency() - .execute(self.storage) - .await?; - - if execution_result.rows_affected() > 0 { - total_rows_affected += execution_result.rows_affected(); - } else { - return Ok(total_rows_affected); - } - } + let execution_result = sqlx::query!( + r#" + DELETE FROM storage_logs + WHERE + storage_logs.miniblock_number < $1 + AND hashed_key IN ( + SELECT + hashed_key + FROM + storage_logs + WHERE + miniblock_number BETWEEN $1 AND $2 + ) + "#, + i64::from(l2_blocks_to_prune.start().0), + i64::from(l2_blocks_to_prune.end().0) + ) + .instrument("hard_prune_batches_range#prune_storage_logs_from_past_l2_blocks") + .with_arg("l2_blocks_to_prune", &l2_blocks_to_prune) + .report_latency() + .execute(self.storage) + .await?; + Ok(execution_result.rows_affected()) } async fn prune_storage_logs_in_range(