Skip to content

perf(db): optimize SQL history pruning to use batching instead of per-insert COUNT(*) #525

Description

@ZhuchkaTriplesix

Problem

In lib/core/storage/local_db.dart, the recordSqlQueryHistory method calls _pruneSqlQueryHistoryBucket on every single executed query. This pruning executes a SELECT COUNT(*) to check if the bucket exceeds maxEntries. Doing an O(N) count query on every history insert adds unnecessary overhead to query execution time as the history table grows.

Solution

Instead of strict pruning on every insert, implement batched lazy-pruning. For example, allow the bucket to grow up to maxEntries + 100, and only run the DELETE cleanup when the threshold is hit, deleting 100 rows in one go. Alternatively, maintain an in-memory counter per session.

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions