$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
650
Stimulusのススメ
patorash
0
79
わかった気になる!OpenID Connect
patorash
2
2.1k
Indexの種類
patorash
1
790
Start-SQLの紹介
patorash
0
740
RailsアプリにGraphQLを導入してみた話
patorash
1
660
Other Decks in Programming
See All in Programming
関数の挙動書き換える
takatofukui
4
770
ローターアクトEクラブ アメリカンナイト:川端 柚菜 氏(Japan O.K. ローターアクトEクラブ 会長):2720 Japan O.K. ロータリーEクラブ2025年12月1日卓話
2720japanoke
0
440
[堅牢.py #1] テストを書かない研究者に送る、最初にテストを書く実験コード入門 / Let's start your ML project by writing tests
shunk031
11
6.9k
ViewファーストなRailsアプリ開発のたのしさ
sugiwe
0
390
30分でDoctrineの仕組みと使い方を完全にマスターする / phpconkagawa 2025 Doctrine
ttskch
3
730
20251127_ぼっちのための懇親会対策会議
kokamoto01_metaps
2
400
Full-Cycle Reactivity in Angular: SignalStore mit Signal Forms und Resources
manfredsteyer
PRO
0
180
令和最新版Android Studioで化石デバイス向けアプリを作る
arkw
0
110
WebRTC、 綺麗に見るか滑らかに見るか
sublimer
1
140
sbt 2
xuwei_k
0
190
これだけで丸わかり!LangChain v1.0 アップデートまとめ
os1ma
6
1.3k
ハイパーメディア駆動アプリケーションとIslandアーキテクチャ: htmxによるWebアプリケーション開発と動的UIの局所的適用
nowaki28
0
330
Featured
See All Featured
A better future with KSS
kneath
240
18k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.5k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
249
1.3M
Building an army of robots
kneath
306
46k
Learning to Love Humans: Emotional Interface Design
aarron
274
41k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
5.7k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.2k
How GitHub (no longer) Works
holman
316
140k
Side Projects
sachag
455
43k
Embracing the Ebb and Flow
colly
88
4.9k
Stop Working from a Prison Cell
hatefulcrawdad
273
21k
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回アクセスしたときの結果をキャッシュして再利用する 頻繁に更新されないもの、且つ頻繁に参照されるものに関して有用(トップページとか) データを更新したときにキャッシュを削除し忘れると事故になるので注意
以上