Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
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
81
わかった気になる!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
公共交通オープンデータ × モバイルUX 複雑な運行情報を 『直感』に変換する技術
tinykitten
PRO
0
160
実は歴史的なアップデートだと思う AWS Interconnect - multicloud
maroon1st
0
260
gunshi
kazupon
1
110
The Art of Re-Architecture - Droidcon India 2025
siddroid
0
120
Spinner 軸ズレ現象を調べたらレンダリング深淵に飲まれた #レバテックMeetup
bengo4com
0
170
AtCoder Conference 2025「LLM時代のAHC」
imjk
2
570
Developing static sites with Ruby
okuramasafumi
0
320
新卒エンジニアのプルリクエスト with AI駆動
fukunaga2025
0
230
Tinkerbellから学ぶ、Podで DHCPをリッスンする手法
tomokon
0
140
Rubyで鍛える仕組み化プロヂュース力
muryoimpl
0
160
ゲームの物理 剛体編
fadis
0
370
AIコーディングエージェント(NotebookLM)
kondai24
0
230
Featured
See All Featured
Building Applications with DynamoDB
mza
96
6.8k
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
30
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
120
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
0
110
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
200
A Tale of Four Properties
chriscoyier
162
23k
Leading Effective Engineering Teams in the AI Era
addyosmani
9
1.4k
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
92
Build The Right Thing And Hit Your Dates
maggiecrowley
38
3k
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
49
Darren the Foodie - Storyboard
khoart
PRO
0
1.9k
The untapped power of vector embeddings
frankvandijk
1
1.5k
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回アクセスしたときの結果をキャッシュして再利用する 頻繁に更新されないもの、且つ頻繁に参照されるものに関して有用(トップページとか) データを更新したときにキャッシュを削除し忘れると事故になるので注意
以上