Implement the following plan: Fix: buildStatsQuery hangs on…
Rate quality
Private research annotation
Sign in to add a private research label or note.
- User
Implement the following plan:
Fix: buildStatsQuery hangs on
/monitoring/users/?cluster=woody&startTime=last30dContext
The users list page (
/monitoring/users/?cluster=woody&startTime=last30d) executes aGROUP BY job.hpc_userstats query withWHERE job.cluster = ? AND job.start_time >= ?. On production databases with hundreds of thousands of jobs in the last 30 days, this query never returns.Root cause: The covering index from migration 12 has column order
(cluster, start_time, hpc_user, ...). Sincestart_timeis a range condition beforehpc_user, SQLite cannot use index ordering forGROUP BY hpc_user. It must collect all matching rows into a temp B-Tree and sort them — extremely expensive for large result sets.Changes
1. Reorder covering index columns (critical fix)
File:
internal/repository/migrations/sqlite3/12_stats-covering-index.up.sqlChange from:
(cluster, start_time, hpc_user, duration, job_state, num_nodes, num_hwthreads, num_acc)Change to:(cluster, hpc_user, start_time, duration, job_state, num_nodes, num_hwthreads, num_acc)This lets SQLite:
- Match
cluster = ?(equality prefix) - Walk
hpc_userin index order (GROUP BY without temp B-Tree) - Within each
(cluster, hpc_user)group, binary-searchstart_time >= ? - Read all aggregation columns from the index (covering)
Add
DROP INDEX IF EXISTS jobs_cluster_starttime_user_statsto handle databases that already applied the old migration 12.File:
internal/repository/migrations/sqlite3/12_stats-covering-index.down.sqlUpdate to drop the renamed index
jobs_cluster_user_starttime_stats.2. Use
COUNT(*)instead ofCOUNT(job.id)File:
internal/repository/stats.goLines to change: 108, 110, 156, 751, 817
COUNT(*)is idiomatic for counting rows and avoids any ambiguity about whether the rowid is available in a covering index scan.3. Fix double-quoted string literals in CASE expressions
File:
internal/repository/stats.goLines 147, 647: Change to
- Match
Assistant
Let me read the files to modify.
Assistant
Now I'll apply all the changes in parallel.
Assistant
Now update
COUNT(job.id)inbuildCountQueryand the histogram functions.Assistant
Now let's run the tests.
Assistant
Both tests pass. Let me also run the full repository test suite to make sure nothing else broke.
Assistant
All tests pass. Here's a summary of the changes:
Migration fix (
12_stats-covering-index.up.sql)- Reordered index columns from
(cluster, start_time, hpc_user, ...)to(cluster, hpc_user, start_time, ...) - Added
DROP INDEX IF EXISTS jobs_cluster_starttime_user_statsto handle databases with the old index - Renamed index to
jobs_cluster_user_starttime_stats
This lets SQLite walk
hpc_userin index order forGROUP BYinstead of building a temp B-Tree.SQL cleanup (
stats.go)COUNT(job.id)→COUNT(*)(5 locations) — idiomatic and avoids rowid lookup in covering index scansjob.job_state = "running"→job.job_state = 'running'(2 CASE expressions) — proper SQL string literals instead of double-quoted identifiers
- Reordered index columns from