$30 off During Our Annual Pro Sale. View Details »
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.3k
exists?で起きるN+1問題にSetで対処する
patorash
0
800
DBのメタデータを管理する文化を作る
patorash
0
660
Stimulusのススメ
patorash
0
80
わかった気になる!OpenID Connect
patorash
2
2.1k
Indexの種類
patorash
1
790
Start-SQLの紹介
patorash
0
750
RailsアプリにGraphQLを導入してみた話
patorash
1
660
Other Decks in Programming
See All in Programming
Developing static sites with Ruby
okuramasafumi
0
310
WebRTC、 綺麗に見るか滑らかに見るか
sublimer
1
190
Navigation 3: 적응형 UI를 위한 앱 탐색
fornewid
1
380
LLM Çağında Backend Olmak: 10 Milyon Prompt'u Milisaniyede Sorgulamak
selcukusta
0
130
認証・認可の基本を学ぼう後編
kouyuume
0
240
Navigating Dependency Injection with Metro
l2hyunwoo
1
150
Go コードベースの構成と AI コンテキスト定義
andpad
0
130
チームをチームにするEM
hitode909
0
350
エディターってAIで操作できるんだぜ
kis9a
0
740
ゆくKotlin くるRust
exoego
1
110
愛される翻訳の秘訣
kishikawakatsumi
3
330
【CA.ai #3】ワークフローから見直すAIエージェント — 必要な場面と“選ばない”判断
satoaoaka
0
270
Featured
See All Featured
How STYLIGHT went responsive
nonsquared
100
6k
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
46
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
400
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
0
85
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
7.9k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
21
Ruling the World: When Life Gets Gamed
codingconduct
0
92
The browser strikes back
jonoalderson
0
63
KATA
mclloyd
PRO
33
15k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Designing for Timeless Needs
cassininazir
0
86
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回アクセスしたときの結果をキャッシュして再利用する 頻繁に更新されないもの、且つ頻繁に参照されるものに関して有用(トップページとか) データを更新したときにキャッシュを削除し忘れると事故になるので注意
以上