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
630
Stimulusのススメ
patorash
0
74
わかった気になる!OpenID Connect
patorash
2
2k
Indexの種類
patorash
1
760
Start-SQLの紹介
patorash
0
720
RailsアプリにGraphQLを導入してみた話
patorash
1
650
Other Decks in Programming
See All in Programming
Julia という言語について (FP in Julia « SIDE: F ») for 関数型まつり2025
antimon2
3
980
型付きアクターモデルがもたらす分散シミュレーションの未来
piyo7
0
810
LINEヤフー データグループ紹介
lycorp_recruit_jp
0
800
Using AI Tools Around Software Development
inouehi
0
1.3k
Select API from Kotlin Coroutine
jmatsu
1
190
[初登壇@jAZUG]アプリ開発者が気になるGoogleCloud/Azure+wasm/wasi
asaringo
0
130
既存デザインを変更せずにタップ領域を広げる方法
tahia910
1
240
地方に住むエンジニアの残酷な現実とキャリア論
ichimichi
5
1.3k
5つのアンチパターンから学ぶLT設計
narihara
1
110
明示と暗黙 ー PHPとGoの インターフェイスの違いを知る
shimabox
2
280
エンジニア向け採用ピッチ資料
inusan
0
160
Rubyでやりたい駆動開発 / Ruby driven development
chobishiba
1
340
Featured
See All Featured
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
357
30k
Git: the NoSQL Database
bkeepers
PRO
430
65k
Balancing Empowerment & Direction
lara
1
360
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
233
17k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3.3k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
34
3k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
20
1.3k
Making the Leap to Tech Lead
cromwellryan
134
9.3k
GraphQLとの向き合い方2022年版
quramy
47
14k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
44
2.4k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
124
52k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
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回アクセスしたときの結果をキャッシュして再利用する 頻繁に更新されないもの、且つ頻繁に参照されるものに関して有用(トップページとか) データを更新したときにキャッシュを削除し忘れると事故になるので注意
以上