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
0
70
ActiveRecordの速度改善Tips2020冬
patorash
November 25, 2020
Tweet
Share
More Decks by patorash
See All by patorash
情報共有戦略と戦術
patorash
1
1.2k
exists?で起きるN+1問題にSetで対処する
patorash
0
780
DBのメタデータを管理する文化を作る
patorash
0
640
Stimulusのススメ
patorash
0
75
わかった気になる!OpenID Connect
patorash
2
2k
Indexの種類
patorash
1
770
Start-SQLの紹介
patorash
0
730
RailsアプリにGraphQLを導入してみた話
patorash
1
650
Other Decks in Programming
See All in Programming
Portapad紹介プレゼンテーション
gotoumakakeru
1
130
Terraform やるなら公式スタイルガイドを読もう 〜重要項目 10選〜
hiyanger
13
3.2k
LLMは麻雀を知らなすぎるから俺が教育してやる
po3rin
3
2.2k
Flutter로 Gemini와 MCP를 활용한 Agentic App 만들기 - 박제창 2025 I/O Extended Seoul
itsmedreamwalker
0
140
decksh - a little language for decks
ajstarks
4
21k
あのころの iPod を どうにか再生させたい
orumin
2
2.5k
兎に角、コードレビュー
mitohato14
0
140
CLI ツールを Go ライブラリ として再実装する理由 / Why reimplement a CLI tool as a Go library
ktr_0731
3
1.1k
AWS Serverless Application Model入門_20250708
smatsuzaki
0
110
「リーダーは意思決定する人」って本当?~ 学びを現場で活かす、リーダー4ヶ月目の試行錯誤 ~
marina1017
0
230
Nuances on Kubernetes - RubyConf Taiwan 2025
envek
0
180
DataformでPythonする / dataform-de-python
snhryt
0
180
Featured
See All Featured
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
53
2.9k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
18
1.1k
YesSQL, Process and Tooling at Scale
rocio
173
14k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
161
15k
For a Future-Friendly Web
brad_frost
179
9.9k
Building Flexible Design Systems
yeseniaperezcruz
328
39k
Bash Introduction
62gerente
614
210k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
50
5.5k
Raft: Consensus for Rubyists
vanstee
140
7.1k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
229
22k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
8
470
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回アクセスしたときの結果をキャッシュして再利用する 頻繁に更新されないもの、且つ頻繁に参照されるものに関して有用(トップページとか) データを更新したときにキャッシュを削除し忘れると事故になるので注意
以上