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
71
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
790
DBのメタデータを管理する文化を作る
patorash
0
640
Stimulusのススメ
patorash
0
76
わかった気になる!OpenID Connect
patorash
2
2.1k
Indexの種類
patorash
1
780
Start-SQLの紹介
patorash
0
740
RailsアプリにGraphQLを導入してみた話
patorash
1
660
Other Decks in Programming
See All in Programming
Ruby×iOSアプリ開発 ~共に歩んだエコシステムの物語~
temoki
0
360
「手軽で便利」に潜む罠。 Popover API を WCAG 2.2の視点で安全に使うには
taitotnk
0
880
今だからこそ入門する Server-Sent Events (SSE)
nearme_tech
PRO
3
260
Zendeskのチケットを Amazon Bedrockで 解析した
ryokosuge
3
320
Performance for Conversion! 分散トレーシングでボトルネックを 特定せよ
inetand
0
5.4k
Introducing ReActionView: A new ActionView-compatible ERB Engine @ Rails World 2025, Amsterdam
marcoroth
0
720
CloudflareのChat Agent Starter Kitで簡単!AIチャットボット構築
syumai
2
520
AIと私たちの学習の変化を考える - Claude Codeの学習モードを例に
azukiazusa1
11
4.5k
Platformに“ちょうどいい”責務ってどこ? 関心の熱さにあわせて考える、責務分担のプラクティス
estie
1
240
AIを活用し、今後に備えるための技術知識 / Basic Knowledge to Utilize AI
kishida
22
5.9k
The Past, Present, and Future of Enterprise Java with ASF in the Middle
ivargrimstad
0
190
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
430
Featured
See All Featured
Context Engineering - Making Every Token Count
addyosmani
3
64
Why You Should Never Use an ORM
jnunemaker
PRO
59
9.5k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
The Straight Up "How To Draw Better" Workshop
denniskardys
236
140k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.9k
Rails Girls Zürich Keynote
gr2m
95
14k
Practical Orchestrator
shlominoach
190
11k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
GraphQLの誤解/rethinking-graphql
sonatard
72
11k
It's Worth the Effort
3n
187
28k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3k
How STYLIGHT went responsive
nonsquared
100
5.8k
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回アクセスしたときの結果をキャッシュして再利用する 頻繁に更新されないもの、且つ頻繁に参照されるものに関して有用(トップページとか) データを更新したときにキャッシュを削除し忘れると事故になるので注意
以上