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
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
patorash
November 25, 2020
Programming
81
0
Share
ActiveRecordの速度改善Tips2020冬
patorash
November 25, 2020
More Decks by patorash
See All by patorash
中間管理職はそこそこ楽しい
patorash
0
30
情報共有戦略と戦術
patorash
1
1.3k
exists?で起きるN+1問題にSetで対処する
patorash
0
820
DBのメタデータを管理する文化を作る
patorash
0
680
Stimulusのススメ
patorash
0
88
わかった気になる!OpenID Connect
patorash
2
2.2k
Indexの種類
patorash
1
810
Start-SQLの紹介
patorash
0
750
RailsアプリにGraphQLを導入してみた話
patorash
1
680
Other Decks in Programming
See All in Programming
飯MCP
yusukebe
0
480
Smarter Angular mit Transformers.js & Prompt API
christianliebel
PRO
1
120
KagglerがMixSeekを触ってみた
morim
0
370
The free-lunch guide to idea circularity
hollycummins
0
410
Rethinking API Platform Filters
vinceamstoutz
0
8.5k
ファインチューニングせずメインコンペを解く方法
pokutuna
0
270
今からFlash開発できるわけないじゃん、ムリムリ! (※ムリじゃなかった!?)
arkw
0
180
Nuxt Server Components
wattanx
0
240
The Monolith Strikes Back: Why AI Agents ❤️ Rails Monoliths
serradura
0
150
瑠璃の宝石に学ぶ技術の声の聴き方 / 【劇場版】アニメから得た学びを発表会2026 #エンジニアニメ
mazrean
0
130
見せてもらおうか、 OpenSearchの性能とやらを!
shunta27
1
170
Reactive ❤️ Loom: A Forbidden Love Story
franz1981
2
220
Featured
See All Featured
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
120
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
68
38k
Scaling GitHub
holman
464
140k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
61k
Done Done
chrislema
186
16k
Why Our Code Smells
bkeepers
PRO
340
58k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
320
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
190
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
230
Embracing the Ebb and Flow
colly
88
5k
Ethics towards AI in product and experience design
skipperchong
2
250
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回アクセスしたときの結果をキャッシュして再利用する 頻繁に更新されないもの、且つ頻繁に参照されるものに関して有用(トップページとか) データを更新したときにキャッシュを削除し忘れると事故になるので注意
以上