Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
ActiveRecordの速度改善Tips2020冬
Search
patorash
November 25, 2020
Programming
100
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
ActiveRecordの速度改善Tips2020冬
patorash
November 25, 2020
More Decks by patorash
See All by patorash
中間管理職はそこそこ楽しい
patorash
0
59
情報共有戦略と戦術
patorash
1
1.4k
exists?で起きるN+1問題にSetで対処する
patorash
0
870
DBのメタデータを管理する文化を作る
patorash
0
720
Stimulusのススメ
patorash
0
100
わかった気になる!OpenID Connect
patorash
2
2.2k
Indexの種類
patorash
1
850
Start-SQLの紹介
patorash
0
790
RailsアプリにGraphQLを導入してみた話
patorash
1
710
Other Decks in Programming
See All in Programming
SREの越境 / SRE Collaboration
y0hgi
2
290
選挙速報を多くのユーザーへ 届ける Live Activities 設計
hamayokokuririn
0
210
更なる可用性を求めて、5年間運用したKotlinのアプリケーションをGoでリプレイスする話
ken_tunc
0
430
モバイル交通系ICへのチャージ実例から考える、クロスプラットフォーム開発におけるiOS実機テスト設計とCI運用
yusuga
1
570
Webの地図
yosuke_furukawa
PRO
6
5k
AWS CDKのカスタムリソースでContinuum(旧Security Agent)を実装した話 | Implementing Continuum (formerly Security Agent) via AWS CDK Custom Resource
akihisaikeda
2
120
Vue Fes Japan 2026 タイムテーブル徹底解説
448jp
1
590
App Intentsのビルドプロセスを支える技術
kntkymt
0
490
GemmaをJevのように使ってみる / Use Gemma like Jev
kishida
5
510
テストを司るデーモンに会いに行く 〜隔離した仮想マシンでテストを通すまで〜
h1d3mun3
1
600
UPDATE をやめる — EF Core でマスタをバージョン管理する
panda728
PRO
0
1.1k
JPUG勉強会 OSSデータベースの内部構造を理解しよう(第2回)
oga5
0
290
Featured
See All Featured
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
500
Making the Leap to Tech Lead
cromwellryan
135
10k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
290
Bootstrapping a Software Product
garrettdimon
PRO
306
120k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6.1k
RailsConf 2023
tenderlove
30
1.6k
Everyday Curiosity
cassininazir
0
340
Game over? The fight for quality and originality in the time of robots
wayneb77
1
290
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.6k
How to build a perfect <img>
jonoalderson
1
6.1k
SEO for Brand Visibility & Recognition
aleyda
0
4.8k
Amusing Abliteration
ianozsvald
1
320
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回アクセスしたときの結果をキャッシュして再利用する 頻繁に更新されないもの、且つ頻繁に参照されるものに関して有用(トップページとか) データを更新したときにキャッシュを削除し忘れると事故になるので注意
以上