Repository navigation
diag: tag analytics queries by surface so D1 Insights can attribute them - #4
Merged
Merged
Conversation
D1 Insights groups by query text. /public/:site_id and
/dashboard/sites/:id/analytics issue textually identical SQL, so their
execution counts and rows-read merge into a single row. That is precisely why
~114,000 analytics renders a month (~3,800/day, ~2.6/min sustained) currently
have no identifiable source: the data to attribute them is being collapsed
before we ever see it.
A trailing SQL comment splits them. Verified on a 365k-row dataset that the tag
is inert: identical result and byte-identical EXPLAIN QUERY PLAN with and
without it, including after a trailing LIMIT.
Tags five statements per surface — the pageview-count statement (one per
render, so it counts renders) and the three custom_events statements plus the
on-demand json_each query. The custom_events statements are the remaining ~29%
of rows read (~2.48B/month) that the previous PR deliberately left alone;
tagging them shows which surface to fix before writing any more query code.
The `beam:public` vs `beam:app` split answers the open question directly. If it
turns out to be the public dashboard, the fix is likely a robots.txt disallow
and a Cache-Control header rather than further query work.
Guards assert the tags survive: removing one tag from a statement that still
typechecks makes two tests fail, verified.
Test plan:
- npm run typecheck passes
- npm test: 123 tests, 119 pass, 0 fail (4 pre-existing skips)
- npm run test:smoke: 128/128 pass
- Verified against wrangler dev: both dashboards render, the on-demand
endpoint serves correct data, and all three empty states are unaffected.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Owner
Author
Full QA round completedThe initial submission had unit tests, smoke, and basic 200-checks, but skipped the systematic matrix that PR #3 got. That mattered here because the tags were applied by string replacement inside the query batch — if a replacement had landed on the wrong occurrence, panels would silently shift. Now covered. Batch integrity — the one that matteredAgainst a fingerprinted dataset where every dimension has a distinct signature and distinct visitor counts, so a one-index shift is unmistakable: No shift. Every panel holds its own dimension in the right order. Matrix — 23/23
Checks specific to this change
Browser, mobile, error path
Plus the original evidence: tag proven inert (identical result, byte-identical |
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.
Summary
We cut ~54% of D1 rows read in #3, but we still don't know what generates the ~114,000 analytics renders a month (~3,800/day, ~2.6/minute sustained) that multiply every remaining query.
The reason is mundane: D1 Insights groups by query text, and the two dashboards issue textually identical SQL.
So they collapse into one row, and the data that would attribute them is destroyed before it reaches the dashboard. A trailing SQL comment splits them.
Verified inert first
On a 365,000-row dataset, with and without the tag:
29000both ways)EXPLAIN QUERY PLAN—SEARCH pageviews USING COVERING INDEX idx_pageviews_site_timestampLIMITNo plan change, no cost, no behaviour change.
Changes
taggedSql(sql, surface)inlib/analytics.ts, withAnalyticsSurface = 'public' | 'app'custom_eventsstatements — the remaining ~29% of rows read (~2.48B/month) that perf(d1): cut ~54% of D1 rows read from analytics page loads #3 deliberately left alonejson_eachquery, so we also learn how often anyone actually opens the panelbuildEventPropertiesQuery(surface)now takes its calling surfaceWhy the custom_events ones too
#3 left ~2.48B rows/month on the table. Tagging those tells us which surface to fix before anyone writes more query code. If it's all public-dashboard traffic, the answer is probably a
robots.txtdisallow and aCache-Controlheader — not query optimisation.Test plan
npm run typecheckpassesnpm test— 123 tests, 119 pass, 0 fail (4 pre-existing skips)npm run test:smoke— 128/128 passwrangler dev: both dashboards render, on-demand endpoint serves correct data, all three empty states unaffectedGuards are proven, not assumed: removing a single tag from a statement that still typechecks makes two tests fail, and they pass again once restored. That's the realistic regression — a future edit rewriting the query and dropping the tag — and it's caught.
Reading the result
24–48h after deploy, D1 Insights should show the pageview-count statement as two rows instead of one. Their execution counts are the render split. Expect them to sum to roughly the current ~114k.
A strong prior worth stating: 3,800 renders/day on the authenticated dashboard would mean about two people loading analytics ~1,900 times each per day. Implausible — so
beam:publicis the likely answer. But an earlier guess in this investigation (that the 60s self-reload was on the public dashboard, when it was actually on/embed/:siteId) turned out wrong, which is exactly why this measures rather than reasons.🤖 Generated with Claude Code