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
ActiveRecordの速度改善Tips2020冬
Search
patorash
November 25, 2020
Programming
0
67
ActiveRecordの速度改善Tips2020冬
patorash
November 25, 2020
Tweet
Share
More Decks by patorash
See All by patorash
情報共有戦略と戦術
patorash
1
1.2k
exists?で起きるN+1問題にSetで対処する
patorash
0
770
DBのメタデータを管理する文化を作る
patorash
0
610
Stimulusのススメ
patorash
0
72
わかった気になる!OpenID Connect
patorash
2
2k
Indexの種類
patorash
1
760
Start-SQLの紹介
patorash
0
710
RailsアプリにGraphQLを導入してみた話
patorash
1
640
Other Decks in Programming
See All in Programming
プロフェッショナルとしての成長「問題の深掘り」が導く真のスキルアップ / issue-analysis-and-skill-up
minodriven
8
2k
AWS Summit Hong Kong 2025: Reinventing Programming - How AI Transforms Our Enterprise Coding Approach
dwchiang
0
140
fieldalignmentから見るGoの構造体
kuro_kurorrr
0
140
状態と共に暮らす:ステートフルへの挑戦
ypresto
3
1.2k
バイラテラルアップサンプリング
fadis
3
520
ぽちぽち選択するだけでOSSを読めるVSCode拡張機能
ymbigo
14
6.3k
CursorとDevinが仲間!?AI駆動で新規プロダクト開発に挑んだ3ヶ月を振り返る / A Story of New Product Development with Cursor and Devin
rkaga
2
730
オープンソースコントリビュート入門
_katsuma
0
130
On-the-fly Suggestions of Rewriting Method Deprecations
ohbarye
3
5.4k
VibeCoding時代のエンジニアリング
daisuketakeda
0
190
Cloudflare Workersで進めるリモートMCP活用
syumai
5
500
GitHub Copilot for Azureを使い倒したい
ymd65536
1
330
Featured
See All Featured
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
105
19k
Designing for Performance
lara
608
69k
Product Roadmaps are Hard
iamctodd
PRO
53
11k
4 Signs Your Business is Dying
shpigford
183
22k
Rails Girls Zürich Keynote
gr2m
94
13k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
30
2k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
49k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
5
600
Making the Leap to Tech Lead
cromwellryan
133
9.3k
[RailsConf 2023] Rails as a piece of cake
palkan
54
5.5k
Producing Creativity
orderedlist
PRO
344
40k
Reflections from 52 weeks, 52 projects
jeffersonlam
349
20k
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回アクセスしたときの結果をキャッシュして再利用する 頻繁に更新されないもの、且つ頻繁に参照されるものに関して有用(トップページとか) データを更新したときにキャッシュを削除し忘れると事故になるので注意
以上