Learning hours roster aggregate is slow (161 ms, 74% of LearningHoursController#index)

Aperta
#7,396 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
62/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
postgresql, rails, ruby

Direzione di ricerca

Start with app/models/learning_hour.rb and inspect the two roster aggregate methods, then review app/services/learning_hours_dashboard_rows_service.rb and the existing indexes before checking the query plan. The issue suggests a composite index, grouping change, or caching; preserve the casa_org_id scope and use the listed request and system specs to check behavior. Done means the aggregate is faster and cross organization scoping still works.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

📈 Performance Improvement 🗄️ Data db Difficulty: Intermediate Help Wanted Priority: High ruby

Problem

Scout APM, Slow Query Insights (7 days to 2026-11-15): the LearningHour roster aggregate behind LearningHoursController#index is 161 ms per query, 74% of that endpoint's response time. It is new this week.

Root cause

LearningHour.all_volunteers_learning_hours (admins) and supervisor_volunteers_learning_hours (supervisors) in app/models/learning_hour.rb join users, filter on users.casa_org_id and learning_hours.occurred_at, then GROUP BY users.display_name, users.id and SUM(...). learning_hours only has an index on user_id, so the occurred_at range filter scans every learning hour for the matching users. Cost grows with the number of entries ever logged.

The controller then does rows.to_a, searches and paginates in Ruby (app/controllers/learning_hours_controller.rb). That is fine at roster size, but the aggregate still runs in full on every page view, including every pagination click.

Suggested fix (pick one or combine)

  • Run EXPLAIN (ANALYZE, BUFFERS) on the query in a production console or a copy of the data and confirm a seq scan on learning_hours.
  • Add a composite index add_index :learning_hours, [:user_id, :occurred_at], algorithm: :concurrently (Strong Migrations requires disable_ddl_transaction!). Check the plan again afterwards.
  • Group by users.id only and select MAX(users.display_name) (or use users.id in the GROUP BY with users.display_name functionally dependent, which Postgres allows when id is the primary key). This avoids sorting and grouping on a text column.
  • Optional: cache the per-volunteer totals for the selected period for a few minutes (Rails.cache.fetch, keyed by org, role and period), so pagination clicks do not re-run the aggregate.
  • Keep the cross-org check: the query must still be scoped to casa_org_id. Run spec/requests/learning_hours_spec.rb and spec/system/learning_hours/.

Related: #6505 (ensure indexes present).

Difficulty: intermediate. Files: app/models/learning_hour.rb, app/services/learning_hours_dashboard_rows_service.rb, a new migration under db/migrate/.

Lingua principale
Ruby
Stelle
384
Fork
558
Merge medio
1g 7h
PR unite (30g)
63

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di rubyforgood/casa

Tutte le issue di rubyforgood/casa

Issue simili

Altre issue su Ruby

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.