← Back to PureKit

Why Filtering 32.4M SQLite Rows by Filename Took 160 Seconds

#SQLite#Query Tuning#Indexing#Profiling
Why Filtering 32.4M SQLite Rows by Filename Took 160 Seconds

The Symptom

Searching a single day’s 9.7 GiB SQLite audit database (32,462,924 rows) by filename took 158–164 seconds just to return 11 rows—even though the host had 133 GB of available RAM so the entire database sat comfortably in OS page cache.

Three Compounding Query Flaws

  1. Leading-wildcard LIKE '%name%': Wrapping the user’s filter in leading and trailing % made the existing B-tree index on object_name completely unusable, forcing row-by-row comparison across 32.4M rows.
  2. Zero-selectivity time predicate: Because databases were already partitioned by day and the UI defaulted to 00:00 through now, the timestamp predicate filtered out virtually nothing.
  3. Duplicate full-table scan: Every page request ran the exact same scan twice—once for ORDER BY ... LIMIT (~124s) and a second time for COUNT(*) (~36s).
  4. Legacy engine constraint: The node ran SQLite 3.26.0 (from 2018), which lacked FTS5 trigram indexes (introduced in 3.34), ruling out drop-in substring indexing.

Pragmatic Optimization

By combining single-pass scan restructuring and adaptive index-vs-table-lookup selection based on estimated selectivity, we brought end-to-end latency down from ~160 seconds to 40–54 seconds without breaking existing schema compatibility.

Enjoyed this note or seen a similar bug?

Feel free to drop a line—always glad to exchange ideas with fellow engineers.

Say hi · [email protected]