Skip to content

Conversation

@Shaptic
Copy link
Contributor

@Shaptic Shaptic commented Dec 23, 2025

What

Ensures that we run query planner optimization at the end of every ledger ingest.

Stray optimization also included: since Core manages ledger entries now, the ledgerEntries field of the db cache was unused entirely, so it has been removed.

Why

See details in faa3d51 and this Slack thread.

Known limitations

It is unclear what the upper bound on this PRAGMA is: obviously if it exceeds ledger close time this is hugely problematic. The alternative is to ask DB operators to run ANALYZE events regularly which seems silly.

This is recommended practice for SQLite databases with short-lived
connections such as the connection for ingestion:

  https://sqlite.org/pragma.html#pragma_optimize

Note the callout of "low-quality indexes" on this page:

  https://sqlite.org/queryplanner-ng.html#howtofix

Due to the nature of both topics and contract IDs, we have a huge
variance in the K -> V ratio. Things like XLM have tons of event
rows given fee events occur on every txn, and "transfer" is
obviously a popular topic1 symbol. This explicitly breaks an
indexing rule:

> A low-quality index is one where there are **more than 10 or 20
> rows in the table that have the same value** for the
> left-most column of the index.

The solution is running `ANALYZE events;`, but this would take too
long and grabs an exclusive lock on the DB, so we must periodically
run `ANALYZE` via the `PRAGMA optimize;` directive which we
execute at the end of every ingestion commit.
@Shaptic Shaptic requested review from a team and tamirms December 23, 2025 20:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants