← Statpro dev log

A slow first visit, a window function and a cache warmer that never ran

#sports#web#product

A waterfall of staggered bars with a glowing amber marker moved far to the left of a dashed line where it used to be, and an arrow showing the shift

The first visitor of the hour to the Insights board on Statpro waited on a chain of database queries, one of them a window function scanning a player's whole career. And every public page waited on a session read before painting a single pixel.

Statpro is a sports stats site, and the Insights board is its public page of betting-research cards. Its results are cached, so the first visitor after the cache goes cold pays for all the work. This week, mostly last night, I took the whole path apart. If you run a Postgres-backed dashboard behind auth, this is the playbook I wish I'd had a week ago.

PROBLEM THREE FIXES RESULT Slow cold board Recency floor + two indexes Warmer that proves it runs Session streams in Suspense Fast first paint first visit of the hour
Three separate causes sat behind one slow page: the query, the cache and the layout.

Measure before you dig

The first commit was pure instrumentation. The query that gathers the pool of trends the board draws from ran its parts serially, so I fanned them out in parallel and cached the pooled result. That alone told me where the rest of the night would go.

The window function was the tax

The query that reads per-game player stats from my shared tables ranked rows with row_number() over a player's entire career. Postgres can't start streaming until it has materialized every log row, ranked them and then filtered. The fix was to shrink the input before the window runs, with a per-league recency floor, a cutoff date before which games are ignored, that still covers the 120-game cap:

-- Simplified. Bound the join before the window function,
-- so Postgres never materializes a whole career of rows.
SELECT *
FROM game_logs g
JOIN games gm
  ON gm.id = g.game_id
 AND gm.game_date >= recency_floor(g.league_slug)  -- MLB/NBA 3y, NFL 9y

The same pass turned up a second waste. An NFL-week subquery ran for every MLB and NBA row, where it always resolved to null. Wrapping it in a CASE WHEN on the league, and skipping the column entirely when the request filters NFL out, cost nothing and paid on every non-football read.

Two indexes, chosen by reading EXPLAIN

Both hot queries were doing parallel sequential scans of the games table, because no index matched their filters. One composite index couldn't serve both, since they filter on different leading columns. Migration 0140 added two: one on (status, game_date) and one on (league, start_time). After the merge, EXPLAIN shows a bitmap index scan on each.

The obvious lesson keeps being the right one. Run EXPLAIN with your real production predicates, and ignore the ones in your head.

The warmer that silently did nothing

With the cold path fixed, I pre-warmed the default cache keys from the web server itself. In production it did nothing, and it said nothing. That failure taught the subtlest lesson of the week.

The warmer's guard read NEXT_RUNTIME off process.env at runtime. The standalone server.js that runs in production never sets it. Only the next CLI does. Next inlines literal process.env.X reads at build time, so a dynamic lookup comes back undefined in the bundled file.

The fix was to read the guard variables as literals in a boot module and pass them along explicitly. One boot log line now proves the warmer resolved a runtime, so the next silent no-op takes seconds to diagnose.

Stop blocking first paint on the session

Meanwhile the session read was stalling every public page. The root layout awaited the session, and for signed-in visitors a lookup of which plan they are on, before returning any markup. That gated every route underneath it.

Now the layout starts the read and never awaits it. The two small UI slots that need the viewer resolve it inside their own React Suspense boundaries, with anonymous-viewer fallbacks. A signed-in visitor never sees their name flip in from an anonymous state, because the fallback renders the same neutral shell the real value replaces. Nothing paints slower in the gated areas, and everything else paints immediately.

Deploys got a pass too

The two image builds now run concurrently, and the deploy fans the stack out in parallel. That cut the measured 28-minute merge-to-live time by about 2.5 minutes. It's a small number, and it compounds on a lane I merge to a dozen times a day.

Make the quiet fix prove it ran

The night added up to stale-while-revalidate on the cold path, two purpose-built indexes, a warmer that provably runs, and a first paint that no longer waits on a session read.

Of those, the warmer is the one I'd check first in any stack. A job that only exists to make things faster can fail without breaking anything, so nothing tells you. Give it one log line that proves it ran.

This is the build log for Statpro (statpro.io) · All entries · Weekly essays