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
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
470
Rails 6.0 Action Mailboxについて
casix
0
410
Other Decks in Technology
See All in Technology
オントロジーって 結局なにもの? AI エージェントに「意味」を渡す知識設計の話
greenmidori
0
190
AI コーディングと人間の認知スループット / CTO Night & Day 2026 Morning Session / AI Coding and Human Cognitive Throughput
prog893
4
1.1k
OptunaHub: ブラックボックス最適化のためのプラットフォーム
pfn
PRO
0
140
⾮構造化データを価値に変える AI エージェント基盤とその組織
ivry_presentationmaterials
0
190
二歩目のフロントエンド・ウェブアクセシビリティ
camiha
2
570
Persisting Massive Data with Rails ~Designing for 60 billion rows, and fighting the bottlenecks~ Railsで挑む大量データ永続化 〜600億件を永続化するための設計とボトルネックとの戦い〜
occhi
0
730
実体験から学ぶ分析エージェントの開発と運⽤
recruitengineers
PRO
8
2.4k
実践! SRE × AI - ABEMA における AIOps -
tetsuya28
8
3.1k
ブラウザとサーバーから考えるAI時代のフロントエンド監視
shogo4131
3
940
フロントエンドカンファレンス関西2026 - 速すぎるUIに、待ち時間を
teamlab
PRO
0
130
USB PDを使ってスマホを労わりたい
raspython3
0
180
Claude Code本は、どのように執筆チームで書かれたか
oikon48
1
380
Featured
See All Featured
Design in an AI World
tapps
1
350
ラッコキーワード サービス紹介資料
rakko
1
5.3M
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.8k
Un-Boring Meetings
codingconduct
0
460
4 Signs Your Business is Dying
shpigford
187
23k
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
370
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
630
Game over? The fight for quality and originality in the time of robots
wayneb77
1
300
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
3.4k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
8.1k
Building a Scalable Design System with Sketch
lauravandoore
464
34k
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