Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
N+1 問題の解決と computed_model
Search
gedorinku
October 05, 2023
Programming
0
22
N+1 問題の解決と computed_model
gedorinku
October 05, 2023
Tweet
Share
More Decks by gedorinku
See All by gedorinku
部内での競プロ用ジャッジシステム
gedorinku
0
1.5k
部内ジャッジを作る話
gedorinku
1
77
プロラボ年度末報告会 HackDay / Hack U 福岡
gedorinku
0
120
Kotlin入門しました
gedorinku
0
260
Other Decks in Programming
See All in Programming
Build Apps for iOS, Android & Desktop in 100% Kotlin With Compose Multiplatform (mDevCamp 2024)
zsmb
0
370
OpenAPIを中心に考えるAPI開発入門 / Introduction to API Development with a Focus on OpenAPI
seike460
PRO
2
170
Hanami and htmx
bkuhlmann
0
210
try! Swift Tokyo 2024 参加報告 / try! Swift Tokyo 2024 Report
hironytic
0
210
dbtのドメイン分割による データ基盤の改善とDigdagとの連携
sakama
0
370
Amazon SQSコンシューマー疎結合への旅 - 出張! #DevelopersIO IT技術ブログの中の人が語る勉強会 #3
quiver
0
280
try!Swift Tokyo 2024 参加報告 LT
akidon0000
1
220
ONE WEDGE_company_guide
1wedge_one
0
500
Elm Form Validation
bkuhlmann
0
510
Ruby GitHub Packages
bkuhlmann
0
630
はてなにおける CSS Modules、及び CSS Modules に足りないもの / CSS Modules in Hatena, and CSS Modules missing parts
mizdra
7
950
Fast JSX: Don't clone props object #28768
yossydev
1
140
Featured
See All Featured
Unsuck your backbone
ammeep
663
57k
Mobile First: as difficult as doing things right
swwweet
216
8.6k
For a Future-Friendly Web
brad_frost
172
9k
Documentation Writing (for coders)
carmenintech
60
3.9k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
60
14k
Intergalactic Javascript Robots from Outer Space
tanoku
266
26k
The World Runs on Bad Software
bkeepers
PRO
61
6.7k
Making the Leap to Tech Lead
cromwellryan
124
8.5k
RailsConf 2023
tenderlove
4
540
The Illustrated Children's Guide to Kubernetes
chrisshort
31
46k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
18
6.9k
The Art of Programming - Codeland 2020
erikaheidi
42
12k
Transcript
© 2023 Wantedly, Inc. N+1 問題の解決と computed_model Oct. 5 2023
- Ryota Egusa (@gedorinku)
Wantedly のアーキテクチャ © 2023 Wantedly, Inc. Rails のマイクロサービス GraphQL のデータソースなどに使われる、汎用
的な API を持つ
加工した値を返すモデルのメソッド class User < ApplicationRecord has_many :work_experiences # => "ウォンテッドリー株式会社
/ エンジニア" def position_description work_experience = work_experiences.max_by { _1.started_at } "#{work_experience.company_name} / #{work_experience.position}" end end © 2023 Wantedly, Inc.
Preload と N+1 問題 class User < ApplicationRecord has_many :work_experiences
… end User.where(...).map(&:position_description) User.preload(:work_experiences).where(...).map(&:position_description) © 2023 Wantedly, Inc.
computed_model による解決方法 class User define_primary_loader :raw_user do ... end define_loader
:work_experiences do ... end dependency :work_experiences computed def position_description work_experience = work_experiences.max_by { _1.started_at } "#{work_experience.company_name} / #{work_experience.position}" end end © 2023 Wantedly, Inc. 依存関係をここに書く ここに書かれていないものを使うと エラーになる
Active Record をデータソースとして使う例 class User define_primary_loader :raw_user do |_, ids:,
**| RawUser.where(id: ids).map do |raw_user| User.new(raw_user) end end define_loader :work_experiences do ... end end © 2023 Wantedly, Inc.
他サービスの API をデータソースとして使う例 class User define_primary_loader :raw_user do ... end
define_loader :work_experiences, key: -> { id } do |user_ids, _, **| WorkExperienceApiClient.list(user_ids: user_ids).group_by(&:user_id) end end © 2023 Wantedly, Inc.
computed_model からデータを読む 指定していないフィールドを使うとエラーになる users = User.batch_get(user_ids, [:position_description]) users.map(&:position_description) users.map(&:name) #
=> error © 2023 Wantedly, Inc.
まとめ 1. 抽象化を損なわず依存関係解決 ◦ N+1問題を防ぎ、必要なデータだけ読み込む 2. データソースには Active Record に限らず
HTTP API なども使える © 2023 Wantedly, Inc. https://github.com/wantedly/computed_model