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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
patorash
November 25, 2020
Programming
0
74
ActiveRecordの速度改善Tips2020冬
patorash
November 25, 2020
Tweet
Share
More Decks by patorash
See All by patorash
中間管理職はそこそこ楽しい
patorash
0
3
情報共有戦略と戦術
patorash
1
1.3k
exists?で起きるN+1問題にSetで対処する
patorash
0
810
DBのメタデータを管理する文化を作る
patorash
0
660
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
今こそ知るべき耐量子計算機暗号(PQC)入門 / PQC: What You Need to Know Now
mackey0225
3
380
HTTPプロトコル正しく理解していますか? 〜かわいい猫と共に学ぼう。ฅ^•ω•^ฅ ニャ〜
hekuchan
2
690
AtCoder Conference 2025
shindannin
0
1.1k
高速開発のためのコード整理術
sutetotanuki
1
410
Patterns of Patterns
denyspoltorak
0
1.4k
izumin5210のプロポーザルのネタ探し #tskaigi_msup
izumin5210
1
140
AgentCoreとHuman in the Loop
har1101
5
240
開発者から情シスまで - 多様なユーザー層に届けるAPI提供戦略 / Postman API Night Okinawa 2026 Winter
tasshi
0
210
MUSUBIXとは
nahisaho
0
140
Automatic Grammar Agreementと Markdown Extended Attributes について
kishikawakatsumi
0
200
CSC307 Lecture 07
javiergs
PRO
1
560
Lambda のコードストレージ容量に気をつけましょう
tattwan718
0
140
Featured
See All Featured
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
330
Information Architects: The Missing Link in Design Systems
soysaucechin
0
780
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
2.7k
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
130
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
0
140
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.7k
Designing Powerful Visuals for Engaging Learning
tmiket
0
240
jQuery: Nuts, Bolts and Bling
dougneiner
65
8.4k
Between Models and Reality
mayunak
1
190
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
1
1.3k
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
120
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回アクセスしたときの結果をキャッシュして再利用する 頻繁に更新されないもの、且つ頻繁に参照されるものに関して有用(トップページとか) データを更新したときにキャッシュを削除し忘れると事故になるので注意
以上