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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
patorash
November 25, 2020
Programming
0
75
ActiveRecordの速度改善Tips2020冬
patorash
November 25, 2020
Tweet
Share
More Decks by patorash
See All by patorash
中間管理職はそこそこ楽しい
patorash
0
24
情報共有戦略と戦術
patorash
1
1.3k
exists?で起きるN+1問題にSetで対処する
patorash
0
810
DBのメタデータを管理する文化を作る
patorash
0
670
Stimulusのススメ
patorash
0
83
わかった気になる!OpenID Connect
patorash
2
2.2k
Indexの種類
patorash
1
800
Start-SQLの紹介
patorash
0
750
RailsアプリにGraphQLを導入してみた話
patorash
1
670
Other Decks in Programming
See All in Programming
AIコーディングの理想と現実 2026 | AI Coding: Expectations vs. Reality 2026
tomohisa
0
570
朝日新聞のデジタル版を支えるGoバックエンド ー価値ある情報をいち早く確実にお届けするために
junkiishida
1
250
個人開発は儲からない - それでも開発開始1ヶ月で300万円売り上げた方法
taishiyade
0
110
nilとは何か 〜interfaceの構造とnil!=nilから理解する〜 / Understanding nil in Go Interface Representation and Why nil != nil
kuro_kurorrr
2
1.2k
CopilotKit + AG-UIを学ぶ
nearme_tech
PRO
1
110
Metaprogramming isn't real, it can't hurt you
okuramasafumi
0
130
あなたはユーザーではない #PdENight
kajitack
4
280
生成AIを活用したソフトウェア開発ライフサイクル変革の現在値
hiroyukimori
PRO
0
140
今、アーキテクトとして 品質保証にどう関わるか
nealle
0
180
並行開発のためのコードレビュー
miyukiw
2
2k
AIによる開発の民主化を支える コンテキスト管理のこれまでとこれから
mulyu
3
2k
CSC307 Lecture 07
javiergs
PRO
1
560
Featured
See All Featured
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
1
140
Music & Morning Musume
bryan
47
7.1k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
9.7k
Building a Scalable Design System with Sketch
lauravandoore
463
34k
For a Future-Friendly Web
brad_frost
183
10k
The Curse of the Amulet
leimatthew05
1
9.1k
AI: The stuff that nobody shows you
jnunemaker
PRO
3
320
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
74
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
130
How To Stay Up To Date on Web Technology
chriscoyier
791
250k
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
180
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
190
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回アクセスしたときの結果をキャッシュして再利用する 頻繁に更新されないもの、且つ頻繁に参照されるものに関して有用(トップページとか) データを更新したときにキャッシュを削除し忘れると事故になるので注意
以上