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
110
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
63
情報共有戦略と戦術
patorash
1
1.4k
exists?で起きるN+1問題にSetで対処する
patorash
0
880
DBのメタデータを管理する文化を作る
patorash
0
720
Stimulusのススメ
patorash
0
100
わかった気になる!OpenID Connect
patorash
2
2.2k
Indexの種類
patorash
1
850
Start-SQLの紹介
patorash
0
800
RailsアプリにGraphQLを導入してみた話
patorash
1
720
Other Decks in Programming
See All in Programming
Coding with AI: How to trust the storyteller (AWS Community Day Adria, Ljubljana 2026)
slobodan
0
120
動画プレイヤーはなぜカクつくのか 〜Flutterのレンダリングとネイティブの境界を解剖する〜
naruogram
0
110
Snowflake Openflow Connector利用の勘所 / 20261024developersio2026tokyo-snowflake-openflow
kasacchiful
PRO
0
190
パスワード再設定メールのURLについて知っておこう
uyuki234
0
190
自作Discord音声botをLambda MicroVMsに移行した話 / Migrating My Discord Voice Bot to Lambda MicroVMs
akihisaikeda
0
150
続・コーディングルールの鮮度を保ちたい / keep-fresh-go-internal-conventions-again
handlename
0
130
【加筆修正版】Laravel のアプリケーションをどこにデプロイするか #phpcon_ehime
akase244
0
160
エージェントを使い続けるためのメタワーク / Meta-work for keeping using the agent
rkaga
6
1.9k
AIで開発が速くなった先で、開発チームは何を目指すか
shihochan
1
1.8k
現場と育てるRailsモデリング 〜Spreadsheetから始めたモデル設計〜
siroemk
2
960
クラウドエージェントは、 何がそんなに便利なのか / what makes cloud agents so useful
rkaga
3
560
When benchmarks go bad - what I learned from measuring performance wrong
hollycummins
0
240
Featured
See All Featured
First, design no harm
axbom
PRO
2
1.4k
The Invisible Side of Design
smashingmag
301
52k
We Have a Design System, Now What?
morganepeng
55
8.4k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
470
The Cult of Friendly URLs
andyhume
79
7k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
940
What does AI have to do with Human Rights?
axbom
PRO
1
2.5k
The Language of Interfaces
destraynor
162
27k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
1k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
Navigating Team Friction
lara
192
16k
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
2
490
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回アクセスしたときの結果をキャッシュして再利用する 頻繁に更新されないもの、且つ頻繁に参照されるものに関して有用(トップページとか) データを更新したときにキャッシュを削除し忘れると事故になるので注意
以上