Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Caching associations in rails
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
horikawa
August 14, 2019
Technology
340
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Caching associations in rails
horikawa
August 14, 2019
More Decks by horikawa
See All by horikawa
Create Action Mailbox Sample Application
casix
1
450
Rails 4.2→5.2アップデート
casix
0
460
Rails 6.0 Action Mailboxについて
casix
0
410
Other Decks in Technology
See All in Technology
アプリをもっと"iOSアプリっぽく"する小さな工夫 / Small Touches That Make Your App Feel More Like an iOS App
matsuji
2
1k
beyond jj: config & tools ecosystem
indirect
0
5.7k
おい、エージェントを使って終わらせろ
nwiizo
2
840
開発投資の期待値を上げるプロダクトロードマップづくり ~プロダクトエンジニアが越境して事業を伸ばす~
kekekenta
1
240
AIに賢く動いてもらうためのコンテキスト〜Snowflake女子会 vol.8
snowwmn0824
0
140
SREへの勘違いに気づいた後の話
tomodakengo
0
120
2026-09-08 そのJavaモダナイゼーション、AIに丸投げで大丈夫?IBM Bobで変わる品質と効率
yutanonaka
1
120
Slack上でインフラをトラブルシュートする! Agentic Platform Engineeringの第一歩
teru0x1
5
1.8k
What the customer really needed
kawaguti
PRO
3
220
データ_AIの事業の勝敗をわけるもの
nek0128
1
470
ソフトウェアDNAとクラウドエージェントのススメ
cloudace
0
120
AgentCore Runtime上にAgentic Coding基盤を構築・展開する際の設計ポイントと限界点 / Design considerations and limitations when building an agentic coding platform on AgentCore Runtime
har1101
5
610
Featured
See All Featured
Marketing to machines
jonoalderson
1
5.8k
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
910
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
560
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
470
Navigating Weather and Climate Data
rabernat
0
520
The browser strikes back
jonoalderson
0
1.7k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
53k
Designing for humans not robots
tammielis
254
26k
A better future with KSS
kneath
240
18k
Ruling the World: When Life Gets Gamed
codingconduct
0
340
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.8k
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
2
280
Transcript
AssociationͷΩϟογϡ ӡ༻ʹؾΛ͚ͭͯ (Otemachirb#20) גࣜձࣾπΫϧόɹαʔόαΠυΤϯδχΞ ງɹ@CASIXx1
• ීஈʮcowcamoʯͱ͍͏ΞϓϦΛ։ൃͯ͠ ·͢ • αʔόαΠυɺRailsʢ5.2ʣΛͬͯ·͢
ISUCON͍ۙͰ͢Ͷ
ISUCONͷաڈ ͬͯΔͷͰ͕͢
N + 1 ఆ൪Ͱ͢ΑͶɻɻɻ
෮शʣN + 1ͱʁ
ҎԼͷϞσϧ͕͋ͬͨ߹ app/models/user.rb class User < ApplicationRecord has_many :memos
end app/models/memo.rb class Memo < ApplicationRecord belongs_to :user end
͜ΕΛ࣮ߦ͢Δͱ… @memos = Memo.all @memos.each do |memo| memo.id
memo.user.name end
N + 1͕ൃੜ͢Δ N(memo.userͷ) + 1(Memo.allͷΞΫηε) SELECT 'memos'.* FROM 'memos'
# Memo.allͷΞΫηε # memo.user.nameͷΞΫηε SELECT 'users'.* FROM 'users' WHERE 'users'.'id' = 1 LIMIT 1 SELECT 'users'.* FROM 'users' WHERE 'users'.'id' = 2 LIMIT 1 SELECT 'users'.* FROM 'users' WHERE 'users'.'id' = 3 LIMIT 1
RailsͬͯΔͱɺ N + 1Λ؆୯ʹղফͰ͖·͢ΑͶ
includes, preload, eager_load Λ͑ྑ͍ @memos = Memo.all.includes(:user) @memos.each
do |memo| memo.id memo.user.name end SELECT 'memo'.* FROM 'memos' SELECT 'users'.* FROM 'users' WHERE 'users'.'id' IN (1, 2, 3) N + 1→1 + 1ʹͳΔ
includesͱ͔eager_loadͱ͔ ৭ʑ͋Γ·͢ΑͶ
N + 1ΛղܾͰ͖Δ͔൱͔ • N + 1ΛղܾͰ͖ͳ͍ • joins •
N + 1ΛղܾͰ͖Δʢؔ࿈ςʔϒϧใΛؚΊͯΩϟογϡ͢Δʣ • eager_load • LEFT OUTER JOINΛར༻͢Δʢt0_r0Έ͍ͨͳͭʣ • preload • IN۟Λར༻ͯ͠ɺෳΫΤϦͰऔͬͯ͘Δ • whereͰؔ࿈ςʔϒϧͷใΛߜΕͳ͍ • includes • eager_load, preloadΛঢ়گʹԠͯ͡ɺ͍͚͢Δ
AssociationͷΩϟογϡ ສೳ͔??
ύϑΥʔϚϯεతʹ ؾΛ͚ͭͳ͚ΕͳΒͳ͍͋ΔΒ͍͕͠…
ӡ༻໘ͰؾΛ͚ͭͳ͍ͱ͍͚ͳ͍ʂ
ؔ࿈ઌςʔϒϧͷΧϥϜΛ আ͍ͨ͠ͱ͢Δ • UserςʔϒϧͰɺআ͍ͨ͠ΧϥϜ͕͋Δͱ͢Δ • Ծʹɺ deprecated_columnͱ͍͏໊લʹ͠·͢ • Α͠ʂΧϥϜআ͢Δ ϚΠάϨʔγϣϯΛ͔͚Α͏ʂ
• ͔͠͠ɺΤϥʔ͕ग़ͯ͠·͍·ͨ͠ɻ ԿނͰ͠ΐ͏ʁʁ
ؔ࿈ઌςʔϒϧͷΧϥϜΛআ ͍ͨ͠ͱ͢Δ • લఏ݅ • ↓ͷίʔυ͕ଘࡏ͢Δ • ΞϓϦέʔγϣϯίʔυ͔Βɺ deprecated_columnΛࢀর͠ͳ͍Α͏ʹमਖ਼ࡁΈ
@memos = Memo.includes(:user).where(users: { name: ‘horikawa’ }) @memos.each do |memo| memo.id memo.user.name end
͑ • ؔ࿈ςʔϒϧͷΩϟογϡΛऔಘ͢Δࡍʹɺ શͯͷΧϥϜใΛSELECTจͰࢦఆͨ͠ΫΤϦΛൃߦ͠Α͏ͱ͢Δ (SELECT … `users`.`id` AS t1_r0, `users`.`deprecated_column`
AS t1_r1) ↓ • ϚΠάϨʔγϣϯͰɺΧϥϜΛআ͢Δ ˣ • ΫΤϦΛ࣮ߦ͢ΔࡍʹɺΩϟογϡΛ͏ͷͰɺ ଘࡏ͠ͳ͍ΧϥϜʹର͢ΔΞΫηεΛͯ͠ɺΤϥʔ͕ग़Δ
͑ # whereͰ݅ذ͢Δͱɺleft outer joinͰΩϟογϡΛੜ͢Δ @memos = Memo.includes(:user).where(users:
{ name: ‘horikawa’ }) # ॲཧ͕͜͜Βลʹ͍ΔλΠϛϯάͰɺΧϥϜΛআ͞Εͯ͠·͏ͱ… # memo.deprecated_column͕ͳ͍ͱΤϥʔ͕ग़Δ @memos.each do |memo| memo.id memo.user.name end
ରࡦ
Rails 5.xͰ͋Ε • ignored_columnsΛ͓͏ʂ • εΩʔϚใʹՃ͑ͳ͍Α͏ʹͯ͘͠ΕΔ • User.column_namesͰ deprecated_column͕ग़ͳ͚Ε͓k
class User < ApplicationRecord self.ignored_columns = %w(deprecated_column) end
ਖ਼͍͠σϓϩΠॱ • deprecated_columnΛࢀর͢ΔίʔυΛআ͢ Δ ˣ • ignored_columnʹdeprecated_columnΛՃ ˣ • ΧϥϜΛআ
ͪͳΈʹRails 4.xͰ͋Ε • ActiveRecord::Base.columnsΛΦʔόʔϥΠ υͯ͠ɺ deprecated_columnΛreject͢Δඞཁ͕͋Δ Έ͍ͨͰ͢
Rails ্͛Α͏
ࢀߟ • https://moneyforward.com/engineers_blog/2019/04/02/ activerecord-includes-preload-eagerload/ • https://dev.classmethod.jp/server-side/activerecord-join/ • https://qiita.com/ykamez/items/0c81a33ec1b90219d541 • https://qiita.com/SoarTec-lab/items/
a192d381323cb741538e • https://eagletmt.hateblo.jp/entry/2017/09/24/004709