From fd9008f1239d614c347d6c316ed4f93b113d1259 Mon Sep 17 00:00:00 2001 From: Jussi Saurio Date: Mon, 6 Oct 2025 10:11:19 +0300 Subject: [PATCH] MVCC: do checkpoint writes in ascending order of rowid For insert-heavy checkpoints this gives a much higher chance of using the balance-quick subalgorithm instead of the more complex and slower balance-nonroot. --- core/mvcc/database/checkpoint_state_machine.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/core/mvcc/database/checkpoint_state_machine.rs b/core/mvcc/database/checkpoint_state_machine.rs index a207d5ad2..7953cf2e4 100644 --- a/core/mvcc/database/checkpoint_state_machine.rs +++ b/core/mvcc/database/checkpoint_state_machine.rs @@ -253,6 +253,16 @@ impl CheckpointStateMachine { } } } + // Writing in ascending order of rowid gives us a better chance of using balance-quick algorithm + // in case of an insert-heavy checkpoint. + self.write_set.sort_by_key(|version| { + ( + // Sort by table_id descending (schema changes first) + std::cmp::Reverse(version.0.row.id.table_id), + // Then by row_id ascending + version.0.row.id.row_id, + ) + }); self.checkpointed_txid_max_new = max_timestamp; }