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
Caching associations in rails
Search
horikawa
August 14, 2019
Technology
1
310
Caching associations in rails
horikawa
August 14, 2019
Tweet
Share
More Decks by horikawa
See All by horikawa
Create Action Mailbox Sample Application
casix
1
290
Rails 4.2→5.2アップデート
casix
0
340
Rails 6.0 Action Mailboxについて
casix
0
350
Other Decks in Technology
See All in Technology
TechFeed Experts Night#27 〜 フロントエンドフレームワーク最前線 (Svelte)
baseballyama
1
530
MapLibreとAmazon Location Service
dayjournal
1
160
Delivering Millions of Messages within seconds @ Duolingo
pelelgrino
0
350
JSON攻略法.pdf
miyakemito
8
5.1k
自己改善からチームを動かす! 「セルフエンジニアリングマネージャー」のすゝめ
shoota
6
780
今年のRubyKaigiはProfiler Year🤘
osyoyu
0
170
障害対応をちょっとずつよくしていくための 演習の作りかた
heleeen
0
230
MLOpsの「壁」を乗り越える、LINEヤフーの Data Quality as Code
lycorptech_jp
PRO
5
530
AWSに詳しくない人でも始められるコスト最適化ガイド
yuhta28
1
250
エンジニアのキャリアをちょっと楽しくする3本の軸/Three Pillars to Make an Engineer's Career More Enjoyable
kwappa
0
2.7k
Vertex AI を中心に 生成AIのアップデートを共有します
kaz1437
0
310
複雑な構成要素を持つUIとの向き合い方 〜新・支出グラフでの実例〜 / B43 TECH TALK
nakamuuu
0
140
Featured
See All Featured
A Modern Web Designer's Workflow
chriscoyier
689
190k
Become a Pro
speakerdeck
PRO
11
4.5k
Scaling GitHub
holman
457
140k
The Cult of Friendly URLs
andyhume
74
5.7k
Navigating Team Friction
lara
178
13k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
125
32k
Robots, Beer and Maslow
schacon
PRO
155
7.9k
Design by the Numbers
sachag
274
18k
Keith and Marios Guide to Fast Websites
keithpitt
408
22k
A better future with KSS
kneath
231
16k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
14
1.5k
How to Ace a Technical Interview
jacobian
272
22k
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