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
75
わかった気になる!OpenID Connect
patorash
2
2k
Indexの種類
patorash
1
770
Start-SQLの紹介
patorash
0
720
RailsアプリにGraphQLを導入してみた話
patorash
1
650
Other Decks in Programming
See All in Programming
AI駆動のマルチエージェントによる業務フロー自動化の設計と実践
h_okkah
0
150
Hack Claude Code with Claude Code
choplin
4
2.1k
#QiitaBash MCPのセキュリティ
ryosukedtomita
1
1.3k
Azure AI Foundryではじめてのマルチエージェントワークフロー
seosoft
0
170
Google Agent Development Kit でLINE Botを作ってみた
ymd65536
2
250
たった 1 枚の PHP ファイルで実装する MCP サーバ / MCP Server with Vanilla PHP
okashoi
1
260
RailsGirls IZUMO スポンサーLT
16bitidol
0
190
猫と暮らす Google Nest Cam生活🐈 / WebRTC with Google Nest Cam
yutailang0119
0
120
Agentic Coding: The Future of Software Development with Agents
mitsuhiko
0
100
#kanrk08 / 公開版 PicoRubyとマイコンでの自作トレーニング計測装置を用いたワークアウトの理想と現実
bash0c7
1
770
ruby.wasmで多人数リアルタイム通信ゲームを作ろう
lnit
3
490
The Modern View Layer Rails Deserves: A Vision For 2025 And Beyond @ RailsConf 2025, Philadelphia, PA
marcoroth
1
170
Featured
See All Featured
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
30
2.1k
GraphQLとの向き合い方2022年版
quramy
49
14k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Stop Working from a Prison Cell
hatefulcrawdad
271
21k
Building Flexible Design Systems
yeseniaperezcruz
328
39k
[RailsConf 2023] Rails as a piece of cake
palkan
55
5.7k
Balancing Empowerment & Direction
lara
1
430
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
281
13k
A Tale of Four Properties
chriscoyier
160
23k
Music & Morning Musume
bryan
46
6.6k
The Cult of Friendly URLs
andyhume
79
6.5k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
130
19k
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回アクセスしたときの結果をキャッシュして再利用する 頻繁に更新されないもの、且つ頻繁に参照されるものに関して有用(トップページとか) データを更新したときにキャッシュを削除し忘れると事故になるので注意
以上