Skip to content

Commit

Permalink
revert(pruning): Revert pruning query (#2220)
Browse files Browse the repository at this point in the history
## What ❔

Reverts #2179

## Why ❔

The new query turned out to be slower than the old one.
  • Loading branch information
slowli authored Jun 12, 2024
1 parent 00c4cca commit 8427cdd
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 61 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

68 changes: 23 additions & 45 deletions core/lib/dal/src/pruning_dal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,51 +318,29 @@ impl PruningDal<'_, '_> {
&mut self,
l2_blocks_to_prune: ops::RangeInclusive<L2BlockNumber>,
) -> DalResult<u64> {
/// 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(
Expand Down

0 comments on commit 8427cdd

Please sign in to comment.