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
97
N+1 問題の解決と computed_model
gedorinku
October 05, 2023
Tweet
Share
More Decks by gedorinku
See All by gedorinku
Active Record Encryption と AWS KMS でエンベロープ暗号化
gedorinku
0
600
Wantedly のバックエンドの将来に向けた取り組みと課題 - Wantedly Tech Night 2024/5
gedorinku
0
140
Porting mruby/c for the SNES (Super Famicom) - RubyKaigi 2024
gedorinku
0
4.6k
部内での競プロ用ジャッジシステム
gedorinku
0
1.7k
部内ジャッジを作る話
gedorinku
1
110
プロラボ年度末報告会 HackDay / Hack U 福岡
gedorinku
0
170
Kotlin入門しました
gedorinku
0
280
Other Decks in Programming
See All in Programming
16年目のピクシブ百科事典を支える最新の技術基盤 / The Modern Tech Stack Powering Pixiv Encyclopedia in its 16th Year
ahuglajbclajep
5
870
クラウドに依存しないS3を使った開発術
simesaba80
0
230
インターン生でもAuth0で認証基盤刷新が出来るのか
taku271
0
170
AgentCoreとHuman in the Loop
har1101
5
180
Patterns of Patterns
denyspoltorak
0
660
コントリビューターによるDenoのすゝめ / Deno Recommendations by a Contributor
petamoriken
0
170
それ、本当に安全? ファイルアップロードで見落としがちなセキュリティリスクと対策
penpeen
7
2.2k
余白を設計しフロントエンド開発を 加速させる
tsukuha
5
970
JETLS.jl ─ A New Language Server for Julia
abap34
2
480
はじめてのカスタムエージェント【GitHub Copilot Agent Mode編】
satoshi256kbyte
0
180
re:Invent 2025 トレンドからみる製品開発への AI Agent 活用
yoskoh
0
670
Findy AI+の開発、運用におけるMCP活用事例
starfish719
0
2.2k
Featured
See All Featured
XXLCSS - How to scale CSS and keep your sanity
sugarenia
249
1.3M
The browser strikes back
jonoalderson
0
310
GraphQLとの向き合い方2022年版
quramy
50
14k
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
Balancing Empowerment & Direction
lara
5
840
Accessibility Awareness
sabderemane
0
37
The Illustrated Children's Guide to Kubernetes
chrisshort
51
51k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
99
4 Signs Your Business is Dying
shpigford
187
22k
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
Information Architects: The Missing Link in Design Systems
soysaucechin
0
740
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
0
1.1k
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