perf(db): index the query log's filters instead of the statistics scans - #283
Merged
Merged
Conversation
Version 17 drops the three indexes no statistic reads since the rollups (ts_domain_client, ts_metrics, ts_upstream) and adds one per query log filter: (doh_token, timestamp), (blocked, timestamp) and (query_type, blocked, timestamp). A query type on its own is read as two runs of the type index, one per verdict, carrying only id and timestamp, merged, and the table is read for the page's rows alone. Page misses on a 1.48 M-row database (main -> this): query log, all 42 bench readings 368 963 -> 159 649 token (quiet) count 23 130 -> 15 type (quiet) page 1 9 812 -> 19 blocked page 20 33 -> 18 type (busy) page 1 12 -> 14 Dashboard and Statistics unchanged. Replay writes 885 858 -> 957 160 pages; file 111 291 -> 92 224 pages. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #283 +/- ##
==========================================
- Coverage 91.36% 91.32% -0.04%
==========================================
Files 31 31
Lines 11691 11590 -101
==========================================
- Hits 10681 10585 -96
+ Misses 1010 1005 -5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Last of the rollup series (#278 benches, #279 log counter, #280 rollups, #281 dashboard, #282 Statistics).
What changes
Schema version 17:
idx_query_logs_ts_domain_client,idx_query_logs_ts_metrics,idx_query_logs_ts_upstream. Since perf(stats): fold the dashboard's readings out of the statistics rollups #281/perf(stats): fold the Statistics page's readings out of the statistics rollups #282 no statistic reads them.(doh_token, timestamp),(blocked, timestamp),(query_type, blocked, timestamp).ANALYZEafterwards.query_logswith a query type as the only filter reads two runs of the type index,blocked = 0andblocked = 1. Each run is already newest first, carries onlyid, timestampand is cut atoffset + limit. The runs are merged, and the table is read for the page's rows only. A negativelimit/offsetfrom/api/logsfollows SQLite's rules.Page misses
logs_page_miss_bench/dashboard_page_miss_bench/stats_page_miss_bench,BENCH_NOW=1789212354, main (38276a6) → this branch.type (busy)page 1 is the only reading that got worse: it reads two runs where the metrics index read one. This was reviewed and accepted.* On the 8d database the tick measured 196 when the bench ran straight after the app migrated in place, with the migration's writes still in the WAL. With v17 applied and checkpointed it measures 190, identical to main, and 184 after
VACUUM.Without
(blocked, timestamp), blocked/allowed page 20 regressed to 102/57, because the metrics index had been answering that filter without a table lookup. The index was added after measuring both options.Cost
VACUUM_FREELIST_RATIO, so the first hourly maintenance runs oneVACUUM(119 603 → 86 876 pages). The 8d database stays at 15%. The migration still needs timing on an appliance.Tests
every_database_opens_to_the_same_query_log_indexes(src/db.rs) opens v9, v10, v11, v16 and fresh databases and requires the same five indexes. It then runs every reader that once named a dropped index withINDEXED BY. It fails with the v17 step disabled. It replaces six tests that asserted the dropped indexes exist.a_query_type_filter_pages_exactly_like_the_table(tests/db_test.rs) checks the two-run merge against the plain statement for 10 page sizes/offsets, including negative ones. It fails if a run is cut atlimitinstead ofoffset + limit, or if the negative-limit guard is removed.the_query_log_filters_seek_their_indexes(tests/stats_page_miss_test.rs) covers token page and count, type page and count, blocked page 20, and type + blocked. Each must read under a tenth of the file. It fails on main (token page 290 of 2 160 pages), and fails when the blocked index can't serve the filter (568 of 2 018).🤖 Generated with Claude Code