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
320
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
380
Rails 4.2→5.2アップデート
casix
0
430
Rails 6.0 Action Mailboxについて
casix
0
390
Other Decks in Technology
See All in Technology
サービスを止めるな! DDoS攻撃へのスマートな備えと最前線の事例
coconala_engineer
1
180
対話型音声AIアプリケーションの信頼性向上の取り組み
ivry_presentationmaterials
3
1k
ビジネス職が分析も担う事業部制組織でのデータ活用の仕組みづくり / Enabling Data Analytics in Business-Led Divisional Organizations
zaimy
1
390
助けて! XからWaylandに移行しないと新しいGNOMEが使えなくなっちゃう 2025-07-12
nobutomurata
2
200
20250708オープンエンドな探索と知識発見
sakana_ai
PRO
4
1k
セキュアなAI活用のためのLiteLLMの可能性
tk3fftk
1
330
AIエージェントが書くのなら直接CloudFormationを書かせればいいじゃないですか何故AWS CDKを使う必要があるのさ
watany
18
7.6k
Autify Company Deck
autifyhq
2
44k
ゼロから始めるSREの事業貢献 - 生成AI時代のSRE成長戦略と実践 / Starting SRE from Day One
shinyorke
PRO
0
110
Introduction to Bill One Development Engineer
sansan33
PRO
0
260
shake-upを科学する
rsakata
7
1k
Deep Security Conference 2025:生成AI時代のセキュリティ監視 /dsc2025-genai-secmon
mizutani
4
2.9k
Featured
See All Featured
Visualization
eitanlees
146
16k
GraphQLの誤解/rethinking-graphql
sonatard
71
11k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Building Adaptive Systems
keathley
43
2.7k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
The Language of Interfaces
destraynor
158
25k
VelocityConf: Rendering Performance Case Studies
addyosmani
332
24k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
10
970
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
331
22k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
The Invisible Side of Design
smashingmag
301
51k
It's Worth the Effort
3n
185
28k
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