Skip to content

fix(store/schema): remove redundant indexes#2115

Merged
Mirko-von-Leipzig merged 3 commits into
nextfrom
krisztian/store-sqlite-remove-redundant-indexes
May 22, 2026
Merged

fix(store/schema): remove redundant indexes#2115
Mirko-von-Leipzig merged 3 commits into
nextfrom
krisztian/store-sqlite-remove-redundant-indexes

Conversation

@kkovaacs
Copy link
Copy Markdown
Contributor

In SQLite, a composite index only supports queries that filter on a leftmost prefix of the indexed columns.
This also means that an index on (a, b, c) makes a separate index on (a) redundant for equality/range lookup starts.

The following indexes seem to be redundant in our store SQLite schema:

  • idx_accounts_id_block on (account_id, block_num DESC) is covered by accounts primary key (account_id, block_num).
  • idx_notes_committed_at on (committed_at) is covered by notes primary key (committed_at, batch_index, note_index).
  • idx_account_storage_account_block on (account_id, block_num) is covered by account_storage_map_values primary key (account_id, block_num, slot_name, key).
  • idx_vault_assets_account_block on (account_id, block_num) is covered by account_vault_assets primary key (account_id, block_num, vault_key).
  • idx_transactions_account_id on (account_id) is covered by idx_transactions_account_block_txid on (account_id, block_num, transaction_id).

kkovaacs added 2 commits May 22, 2026 09:24
With the default Debug implementation for `SchemaHash` it's pretty hard
to update assertions in tests after making changes to the schema.
In SQLite, a composite index only supports queries that filter on a
leftmost prefix of the indexed columns.

This also means that an index on `(a, b, c)` makes a separate index on
`(a)` redundant for equality/range lookup starts.

The following indexes seem to be redundant in our store SQLite schema:

- `idx_accounts_id_block` on `(account_id, block_num DESC)` is covered by
  `accounts` primary key `(account_id, block_num)`.
- `idx_notes_committed_at` on `(committed_at)` is covered by `notes`
  primary key `(committed_at, batch_index, note_index)`.
- `idx_account_storage_account_block` on `(account_id, block_num)` is
  covered by `account_storage_map_values` primary key
  `(account_id, block_num, slot_name, key)`.
- `idx_vault_assets_account_block` on `(account_id, block_num)` is
  covered by `account_vault_assets` primary key
  `(account_id, block_num, vault_key)`.
- `idx_transactions_account_id` on `(account_id)` is covered by
  `idx_transactions_account_block_txid` on
  `(account_id, block_num, transaction_id)`.
@kkovaacs kkovaacs added the no changelog This PR does not require an entry in the `CHANGELOG.md` file label May 22, 2026
@kkovaacs kkovaacs marked this pull request as ready for review May 22, 2026 08:52
@Mirko-von-Leipzig Mirko-von-Leipzig merged commit d7d4f0d into next May 22, 2026
20 checks passed
@Mirko-von-Leipzig Mirko-von-Leipzig deleted the krisztian/store-sqlite-remove-redundant-indexes branch May 22, 2026 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no changelog This PR does not require an entry in the `CHANGELOG.md` file

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants