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
71
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
76
わかった気になる!OpenID Connect
patorash
2
2k
Indexの種類
patorash
1
780
Start-SQLの紹介
patorash
0
730
RailsアプリにGraphQLを導入してみた話
patorash
1
650
Other Decks in Programming
See All in Programming
実用的なGOCACHEPROG実装をするために / golang.tokyo #40
mazrean
1
230
print("Hello, World")
eddie
1
510
Zendeskのチケットを Amazon Bedrockで 解析した
ryokosuge
3
280
Azure SRE Agentで運用は楽になるのか?
kkamegawa
0
1.6k
Improving my own Ruby thereafter
sisshiki1969
1
160
MCPでVibe Working。そして、結局はContext Eng(略)/ Working with Vibe on MCP And Context Eng
rkaga
5
2.1k
AIコーディングAgentとの向き合い方
eycjur
0
260
Processing Gem ベースの、2D レトロゲームエンジンの開発
tokujiros
2
120
More Approvers for Greater OSS and Japan Community
tkikuc
1
110
Swift Updates - Learn Languages 2025
koher
2
450
CSC305 Summer Lecture 12
javiergs
PRO
0
130
Claude Codeで挑むOSSコントリビュート
eycjur
0
200
Featured
See All Featured
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.1k
Done Done
chrislema
185
16k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
33
2.4k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
131
19k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
44
2.5k
The Cult of Friendly URLs
andyhume
79
6.6k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
15
1.6k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
53
2.9k
GraphQLの誤解/rethinking-graphql
sonatard
71
11k
How to Think Like a Performance Engineer
csswizardry
26
1.9k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
7
840
Navigating Team Friction
lara
189
15k
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回アクセスしたときの結果をキャッシュして再利用する 頻繁に更新されないもの、且つ頻繁に参照されるものに関して有用(トップページとか) データを更新したときにキャッシュを削除し忘れると事故になるので注意
以上