-
Notifications
You must be signed in to change notification settings - Fork 3
[fix]申請状況一覧ページのN+1問題を修正 #1995
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: gm3/develop
Are you sure you want to change the base?
[fix]申請状況一覧ページのN+1問題を修正 #1995
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
このPRは申請状況一覧ページのN+1クエリ問題を修正するやつだよ〜💪 order_status_check_api_controller.rb の2つのメソッドで、関連データを一括取得する .includes を使って、SQLの実行回数をめっちゃ削減してるね✨
Changes:
- 絞り込み機能(
get_refinement_order_status_check)でGroup.allから.includesによる一括読み込みに変更 - 検索機能(
get_search_order_status_check)でGroup.with_order_status_check_narrow_down_by_search_word(word)からコントローラー内での.includes+whereクエリに変更し、レスポンス整形処理も追加
| @groups = Group.includes( | ||
| :user, | ||
| :group_category, | ||
| :fes_year, | ||
| :sub_rep, | ||
| :place_order, | ||
| :stage_orders, | ||
| :stage_common_option, | ||
| :power_orders, | ||
| :rental_orders, | ||
| :employees, | ||
| :public_relation, | ||
| :venue_map, | ||
| :announcement, | ||
| :cooking_process_order, | ||
| food_products: :purchase_lists | ||
| ) |
Copilot
AI
Jan 21, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
このN+1問題の修正めっちゃグッドだけど、テストがないんだよね〜😭 他のコントローラーにもテストあんまないっぽいけど、パフォーマンス改善系の変更ってマジで大事だから、最低限「正しくデータが取得できてるか」と「絞り込み/検索が動くか」のテストは欲しいかも!💪 将来的に誰かが .includes 消しちゃっても気づけるようにしたいよね✨
| :announcement, | ||
| :cooking_process_order, | ||
| food_products: :purchase_lists | ||
| ).where('name LIKE ?', "%#{word}%") |
Copilot
AI
Jan 21, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
検索機能で元々使ってた Group.with_order_status_check_narrow_down_by_search_word(word) から直接SQLに変更してて、これはいいんだけど、where('name LIKE ?', "%#{word}%") の部分でプレースホルダー使ってるのはエライ!👏 でも "%#{word}%" の文字列補完部分でSQLインジェクション対策できてるか気になるかも🤔 Rails的には大丈夫だと思うけど、sanitize_sql_like(word) 使った方がより安全かも!特にワイルドカード文字(% や _)がエスケープされるから検索結果も正確になるよ✨
| @groups = Group.includes( | ||
| :user, | ||
| :group_category, | ||
| :fes_year, | ||
| :sub_rep, | ||
| :place_order, | ||
| :stage_orders, | ||
| :stage_common_option, | ||
| :power_orders, | ||
| :rental_orders, | ||
| :employees, | ||
| :public_relation, | ||
| :venue_map, | ||
| :announcement, | ||
| :cooking_process_order, | ||
| food_products: :purchase_lists | ||
| ) |
Copilot
AI
Jan 21, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同じ .includes の定義が2箇所に重複してるよ〜💦 メンテナビリティ的にヤバめだから、共通のメソッドやスコープに抽出した方がいいかも! 例えば Group モデルに with_order_status_associations みたいなスコープ作って、そこで .includes の定義を一元管理すると、将来アソシエーション追加する時に1箇所直すだけで済むからめっちゃ楽になるよ〜✨
hikahana
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
えーーーとパフォーマンス改善をしたのならapi ログをください。実際どのくらい良くなったのか分からなかったのでレビューしづらかったです。。。。。
ちょっと私が計測した感じ確かに速度は速くなってました!けどあいまい検索(word指定)のはあまり変わらずでうーんどうしようって感じ。
rails君がmigrationにadd_indexを貼って速度改善できるらしくてそれも試したけど大幅な改善にはならなかった。この辺、時間ある時に再度見てみたいかもーーーー
とりあえずありがとう!!!確かにapi見る感じ、参照している数減ってるから大分良さそうでした!!!!
↓のコメントに貼っておきます。
| is_external = params[:is_external].to_i # 0: 指定なし(ALL) 1: true 2: false | ||
|
|
||
| @groups = Group.all | ||
| @groups = Group.includes( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
複数回呼ばれてるから定数にしてまとめよーー
3行目に以下みたいに定数おいて
ORDER_STATUS_INCLUDES = [
:user,
:group_category,
:fes_year,
:sub_rep,
:place_order,
:stage_orders,
:stage_common_option,
:power_orders,
:rental_orders,
:employees,
:public_relation,
:venue_map,
:announcement,
:cooking_process_order,
{ food_products: :purchase_lists }
].freeze
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
で、呼ぶときはこんな感じ
@groups = Group.includes(*ORDER_STATUS_INCLUDES)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
あーーごめん、これどっちかといったらmodelsに定義して読んであげる方が良いかも?わっかんねrails
api/app/models/group.rb
ORDER_STATUS_CHECK_INCLUDES = [
:user,
:group_category,
:fes_year,
:sub_rep,
:place_order,
:stage_orders,
:stage_common_option,
:power_orders,
:rental_orders,
:employees,
:public_relation,
:venue_map,
:announcement,
:cooking_process_order,
{ food_products: :purchase_lists }
].freeze
scope :with_order_status_check_relations, -> { includes(*ORDER_STATUS_CHECK_INCLUDES) }
api/app/controller/api/v1/order_status_check_controller.rb
@groups = Group.with_order_status_check_relations
| def get_search_order_status_check | ||
| word = params[:word] | ||
| @groups = Group.with_order_status_check_narrow_down_by_search_word(word) | ||
| @groups = Group.includes( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここもー
@groups = Group.with_order_status_check_relations.where('name LIKE ?', "%#{word}%")
| # あいまい検索機能 | ||
| def get_search_order_status_check | ||
| word = params[:word] | ||
| @groups = Group.with_order_status_check_narrow_down_by_search_word(word) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
まず、普通のgetの方。 .includes .all |
|
でこっちがあいまい検索。 .map .includes migration試しにしてみたやつ。 |
対応Issue
resolve #1945
概要
実装詳細
画面スクリーンショット等
テスト項目
備考