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
73
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
810
DBのメタデータを管理する文化を作る
patorash
0
660
Stimulusのススメ
patorash
0
81
わかった気になる!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
Data-Centric Kaggle
isax1015
2
730
0→1 フロントエンド開発 Tips🚀 #レバテックMeetup
bengo4com
0
530
Denoのセキュリティに関する仕組みの紹介 (toranoana.deno #23)
uki00a
0
270
開発者から情シスまで - 多様なユーザー層に届けるAPI提供戦略 / Postman API Night Okinawa 2026 Winter
tasshi
0
180
AIフル活用時代だからこそ学んでおきたい働き方の心得
shinoyu
0
120
CSC307 Lecture 05
javiergs
PRO
0
490
AIエージェントの設計で注意するべきポイント6選
har1101
7
3.3k
Vibe Coding - AI 驅動的軟體開發
mickyp100
0
160
HTTPプロトコル正しく理解していますか? 〜かわいい猫と共に学ぼう。ฅ^•ω•^ฅ ニャ〜
hekuchan
2
670
MUSUBIXとは
nahisaho
0
110
Basic Architectures
denyspoltorak
0
650
カスタマーサクセス業務を変革したヘルススコアの実現と学び
_hummer0724
0
460
Featured
See All Featured
Git: the NoSQL Database
bkeepers
PRO
432
66k
Designing for humans not robots
tammielis
254
26k
Believing is Seeing
oripsolob
1
44
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.1k
Design in an AI World
tapps
0
140
jQuery: Nuts, Bolts and Bling
dougneiner
65
8.4k
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
66
36k
Why Our Code Smells
bkeepers
PRO
340
58k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
600
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
2.7k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
170
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
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回アクセスしたときの結果をキャッシュして再利用する 頻繁に更新されないもの、且つ頻繁に参照されるものに関して有用(トップページとか) データを更新したときにキャッシュを削除し忘れると事故になるので注意
以上