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
85
0
Share
ActiveRecordの速度改善Tips2020冬
patorash
November 25, 2020
More Decks by patorash
See All by patorash
中間管理職はそこそこ楽しい
patorash
0
35
情報共有戦略と戦術
patorash
1
1.3k
exists?で起きるN+1問題にSetで対処する
patorash
0
830
DBのメタデータを管理する文化を作る
patorash
0
690
Stimulusのススメ
patorash
0
88
わかった気になる!OpenID Connect
patorash
2
2.2k
Indexの種類
patorash
1
820
Start-SQLの紹介
patorash
0
760
RailsアプリにGraphQLを導入してみた話
patorash
1
690
Other Decks in Programming
See All in Programming
検索設計から 推論設計への重心移動と Recall-First Retrieval
po3rin
5
1.4k
ハーネスエンジニアリングにどう向き合うか 〜ルールファイルを超えて開発プロセスを設計する〜 / How to approach harness engineering
rkaga
25
17k
AIベース静的検査器の偽陽性率を抑える工夫3選
orgachem
PRO
4
380
JAWS-UG横浜 #100 祝・第100回スペシャルAWS は VPC レスの時代へ
maroon1st
0
200
PHPer、Cloudflare に引っ越す
suguruooki
1
130
Terraform言語の静的解析 / static analysis of Terraform language
wata727
1
120
Symfony AI in Action - SymfonyLive Berlin 2026
chr_hertel
1
110
Programming with a DJ Controller — not vibe coding
m_seki
3
730
Road to RubyKaigi: Play Hard(ware)
makicamel
1
520
HTML-Aware ERB: The Path to Reactive Rendering @ RubyKaigi 2026, Hakodate, Japan
marcoroth
0
590
Oxlintとeslint-plugin-react-hooks 明日から始められそう?
t6adev
0
310
AI時代のエンジニアリングの原則 / Engineering Principles in the AI Era
haru860
0
960
Featured
See All Featured
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
410
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
270
Scaling GitHub
holman
464
140k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
190
How to Ace a Technical Interview
jacobian
281
24k
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
730
KATA
mclloyd
PRO
35
15k
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.1k
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
What does AI have to do with Human Rights?
axbom
PRO
1
2.1k
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
110
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
3
690
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回アクセスしたときの結果をキャッシュして再利用する 頻繁に更新されないもの、且つ頻繁に参照されるものに関して有用(トップページとか) データを更新したときにキャッシュを削除し忘れると事故になるので注意
以上