refactor(store): optimize account_codes pruning#2092
Merged
Conversation
Split `prune_account_codes` SQL query into indexed recent-history and latest-state branches to avoid SQLite scanning account history via code_commitment for the full OR predicate. Add a migration that removes `is_latest` from `idx_accounts_prune_code` for the recent-history branch (not needed) and adds `idx_accounts_latest_code_commitment` for latest account rows.
Mirko-von-Leipzig
approved these changes
May 18, 2026
sergerad
approved these changes
May 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Analyzing traces for current testnet pointed out that there's a serious bottleneck in how we prune
account_codestable:It turns out that SQLite was not using the right index for the SQL subquery we were running to get the set of
code_commitmentvalues that are still required based on account state:This PR splits
prune_account_codesSQL query into indexed recent-history and latest-state branches to avoid SQLite scanning account history viacode_commitmentfor the full OR predicate.To make sure that both branch queries are optimal it also adds a migration that removes
is_latestfromidx_accounts_prune_codefor the recent-history branch (not needed) and addsidx_accounts_latest_code_commitmentfor latest account rows.With these changes applied: