Learning hours roster aggregate is slow (161 ms, 74% of LearningHoursController#index)
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 62/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 領域
- database, performance
調査の方向性
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.
索引モデルが issue の本文から書いたものです。
説明
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 onlearning_hours. - Add a composite index
add_index :learning_hours, [:user_id, :occurred_at], algorithm: :concurrently(Strong Migrations requiresdisable_ddl_transaction!). Check the plan again afterwards. - Group by
users.idonly and selectMAX(users.display_name)(or useusers.idin the GROUP BY withusers.display_namefunctionally dependent, which Postgres allows whenidis 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. Runspec/requests/learning_hours_spec.rbandspec/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/.
- 主要言語
- Ruby
- スター
- 384
- フォーク
- 558
- 平均マージ
- 1日 7時間
- マージ済み PR(30日)
- 63
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile または Docker Compose ファイルあり
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
rubyforgood/casa のほかの issue
-
⛓️ Security Difficulty: Easy Good First Issue Help Wanted ruby Type: Bug
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
rubyforgood/casa#7404 ·
メンテナーはふだん 1 日以内に返信
-
Difficulty: Easy Good First Issue Help Wanted ruby Type: Bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
rubyforgood/casa#7402 ·
メンテナーはふだん 1 日以内に返信
-
Remove the bin/bundle binstub (Heroku warns it can break Bundler on deploy)対応中かも @Akash-Dhanwate が 8 日前に担当しました。 オープンdependencies Difficulty: Easy Good First Issue
難易度 1/5 1時間未満 初心者へのやさしさ 82/100
rubyforgood/casa#7325 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
Reports page: move export-button disable/re-enable into a Stimulus controller再び着手できるかも @Akash-Dhanwate が 9 日前に担当しましたが、オープン中のプルリクエストはありません。 オープンDifficulty: Easy Good First Issue Help Wanted javascript
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
rubyforgood/casa#7311 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
New case: use Stimulus for the "I don't know the court date yet" toggle再び着手できるかも @Aryankn29 が 9 日前に担当しましたが、オープン中のプルリクエストはありません。 オープンDifficulty: Easy Good First Issue Help Wanted javascript
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
rubyforgood/casa#7310 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
rubyforgood/casa の issue をすべて見る
似ている issue
-
enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
internetee/registry#2984 ·
メンテナーはふだん 6 日以内に返信
-
Redis Lua error `@user_script:52: ERR value is not an integer or out of range` when `window_size` is configured対応中かも @mimosa が今日担当しました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 67/100
riscv/riscv-unified-db#2829 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
docker-library/ruby#534 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100