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
65
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
750
DBのメタデータを管理する文化を作る
patorash
0
600
Stimulusのススメ
patorash
0
68
わかった気になる!OpenID Connect
patorash
2
1.9k
Indexの種類
patorash
1
750
Start-SQLの紹介
patorash
0
700
RailsアプリにGraphQLを導入してみた話
patorash
1
640
Other Decks in Programming
See All in Programming
Serverless Rust: Your Low-Risk Entry Point to Rust in Production (and the benefits are huge)
lmammino
1
110
ファインディの テックブログ爆誕までの軌跡
starfish719
2
1.1k
データの整合性を保つ非同期処理アーキテクチャパターン / Async Architecture Patterns
mokuo
48
17k
Pulsar2 を雰囲気で使ってみよう
anoken
0
240
Spring gRPC について / About Spring gRPC
mackey0225
0
220
color-scheme: light dark; を完全に理解する
uhyo
5
390
Grafana Loki によるサーバログのコスト削減
mot_techtalk
1
130
ARA Ansible for the teams
kksat
0
150
CI改善もDatadogとともに
taumu
0
120
Grafana Cloudとソラカメ
devoc
0
170
Boost Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
0
340
Multi Step Form, Decentralized Autonomous Organization
pumpkiinbell
1
750
Featured
See All Featured
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
9
450
Building Your Own Lightsaber
phodgson
104
6.2k
Speed Design
sergeychernyshev
27
790
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
12
960
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Gamification - CAS2011
davidbonilla
80
5.1k
Automating Front-end Workflow
addyosmani
1368
200k
Building Applications with DynamoDB
mza
93
6.2k
Scaling GitHub
holman
459
140k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
YesSQL, Process and Tooling at Scale
rocio
172
14k
Making Projects Easy
brettharned
116
6k
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回アクセスしたときの結果をキャッシュして再利用する 頻繁に更新されないもの、且つ頻繁に参照されるものに関して有用(トップページとか) データを更新したときにキャッシュを削除し忘れると事故になるので注意
以上