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

Ouverte
#7,396 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

Personne n'a encore pris cette issue.

Évaluation

Difficulté
4/5
Temps estimé
3-5 jours
Accessibilité débutants
62/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
Active
Stack technique
postgresql, rails, ruby

Piste de recherche

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.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

📈 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/.

Langage dominant
Ruby
Étoiles
384
Forks
558
Merge moyen
1 j 7 h
PR mergées (30 j)
63

Préparer son environnement

Ouvrir dans Codespaces

Lance le conteneur de développement du projet dans votre navigateur, avec votre propre compte GitHub.

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de rubyforgood/casa

Toutes les issues de rubyforgood/casa

Issues similaires

Plus d'issues Ruby

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.