Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
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.3k
exists?で起きるN+1問題にSetで対処する
patorash
0
800
DBのメタデータを管理する文化を作る
patorash
0
650
Stimulusのススメ
patorash
0
79
わかった気になる!OpenID Connect
patorash
2
2.1k
Indexの種類
patorash
1
790
Start-SQLの紹介
patorash
0
740
RailsアプリにGraphQLを導入してみた話
patorash
1
660
Other Decks in Programming
See All in Programming
MAP, Jigsaw, Code Golf 振り返り会 by 関東Kaggler会|Jigsaw 15th Solution
hasibirok0
0
210
dnx で実行できるコマンド、作ってみました
tomohisa
0
130
C-Shared Buildで突破するAI Agent バックテストの壁
po3rin
0
180
20 years of Symfony, what's next?
fabpot
2
310
DSPy Meetup Tokyo #1 - はじめてのDSPy
masahiro_nishimi
1
150
新卒エンジニアのプルリクエスト with AI駆動
fukunaga2025
0
140
CloudNative Days Winter 2025: 一週間で作る低レイヤコンテナランタイム
ternbusty
7
1.9k
FluorTracer / RayTracingCamp11
kugimasa
0
180
Microservices rules: What good looks like
cer
PRO
0
550
251126 TestState APIってなんだっけ?Step Functionsテストどう変わる?
east_takumi
0
300
著者と進める!『AIと個人開発したくなったらまずCursorで要件定義だ!』
yasunacoffee
0
110
社内オペレーション改善のためのTypeScript / TSKaigi Hokuriku 2025
dachi023
1
470
Featured
See All Featured
Statistics for Hackers
jakevdp
799
230k
Site-Speed That Sticks
csswizardry
13
990
Making Projects Easy
brettharned
120
6.5k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
380
The Power of CSS Pseudo Elements
geoffreycrofte
80
6.1k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
253
22k
Making the Leap to Tech Lead
cromwellryan
135
9.6k
Typedesign – Prime Four
hannesfritz
42
2.9k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
10
700
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
359
30k
Into the Great Unknown - MozCon
thekraken
40
2.2k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
1
78
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回アクセスしたときの結果をキャッシュして再利用する 頻繁に更新されないもの、且つ頻繁に参照されるものに関して有用(トップページとか) データを更新したときにキャッシュを削除し忘れると事故になるので注意
以上