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
ActiveRecordの速度改善Tips2020冬
Search
patorash
November 25, 2020
Programming
95
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
ActiveRecordの速度改善Tips2020冬
patorash
November 25, 2020
More Decks by patorash
See All by patorash
中間管理職はそこそこ楽しい
patorash
0
57
情報共有戦略と戦術
patorash
1
1.4k
exists?で起きるN+1問題にSetで対処する
patorash
0
870
DBのメタデータを管理する文化を作る
patorash
0
720
Stimulusのススメ
patorash
0
99
わかった気になる!OpenID Connect
patorash
2
2.2k
Indexの種類
patorash
1
850
Start-SQLの紹介
patorash
0
790
RailsアプリにGraphQLを導入してみた話
patorash
1
710
Other Decks in Programming
See All in Programming
DroidKaigi 2026 「個人開発という実験場: Android エンジニアが手にする4つの自由」
slashnephy
0
230
新卒PdEのリアル
ryu1013
1
500
速習iPhone Duo対応
yuukiw00w
1
410
kubernetes コンポーネント開発入門 / 新卒N年目の勉強会&交流会!〜〇〇への誘い〜 #n_study
mazrean
0
240
iOSDC Japan 2026 - Swiftで作って学ぼう!データベース自作入門
kaseken
2
160
スマート反転とウェブアクセシビリティ
camiha
0
210
AI × TiDD / 2026.09.05 Redmine 大阪
tokudiro
1
170
市販E-Readerを乗っ取れ 〜Embedded Swiftで電子ペーパーガジェットを制御する〜
trickart
0
190
WebRTC映像をAirPlayに対応させる挑戦.pdf
monolithic_adam
0
260
Security issues being discussed on Web Platforms
petamoriken
0
940
How I Stole PSI from Android Studio - DroidKaigi2026
worker8
0
130
【高い買い物LT会】初任給で話題の国産フィジカルAIを買った話
akagami
PRO
0
160
Featured
See All Featured
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
300
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
660
Documentation Writing (for coders)
carmenintech
77
5.5k
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
830
The SEO identity crisis: Don't let AI make you average
varn
0
560
Leo the Paperboy
mayatellez
10
2.3k
Balancing Empowerment & Direction
lara
6
1.3k
Why Our Code Smells
bkeepers
PRO
340
58k
Everyday Curiosity
cassininazir
0
320
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7.9k
Agile that works and the tools we love
rasmusluckow
331
22k
How GitHub (no longer) Works
holman
316
150k
Transcript
ActiveRecordの 速度改善Tips 2020 冬 株式会社リゾーム システム開発部 尾古 豊明 2020-11-25(水) 社内勉強会
これは2015年に発表した資料の焼き直し 第9回中国地方DB勉強会 IN 米子での発表資料 https://www2.slideshare.net/chariderpato/heroku-active-record-tips
ActiveRecord高速化の肝 DBへのアクセス回数を減らす 大量のデータを一度に取得しない 使うカラムのデータだけ取得する そもそもDBにアクセスしない
適切なindexを設定する
DBへのアクセス回数を減らすには? パフォーマンス監視ツールを使う(rack-mini-profiler) N+1問題の発生を検知するgemを使う(bullet) counter_cacheを使う(Railsにある機能) eager_load, preload,
includes, left_joins, joinsなどのメソッドを使う 大きいテーブルをjoinすると1回のクエリで済むが逆に性能が悪化するケースもあるので注意
大量のデータを取得しないようにするには? find_each, find_in_batches, in_batchesなどのメソッドを使う デフォルトで1,000件ずつ処理する orderが効かないので注意
ページングを行う(kaminari) 存在チェックには、present?ではなく、exists?を使う present?はモデルのオブジェクト作ってしまっているし、全部取得している select * from ~ where ~ exists?はクエリがシンプル select 1 from ~ where ~
使うカラムのデータだけ取得するには? selectメソッドで絞り込む - articles.select(:id, :title).each do |article| =
link_to article.title, article_path(article) end pluckメソッドで値だけ取得する - articles.pluck(:id, :title).each do |(id, title)| = link_to title, article_path(id) end
そもそもDBにアクセスしないには? フラグメントキャッシュなどを使う 1回アクセスしたときの結果をキャッシュして再利用する 頻繁に更新されないもの、且つ頻繁に参照されるものに関して有用(トップページとか) データを更新したときにキャッシュを削除し忘れると事故になるので注意
以上