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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
patorash
November 25, 2020
Programming
93
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
45
情報共有戦略と戦術
patorash
1
1.3k
exists?で起きるN+1問題にSetで対処する
patorash
0
850
DBのメタデータを管理する文化を作る
patorash
0
700
Stimulusのススメ
patorash
0
95
わかった気になる!OpenID Connect
patorash
2
2.2k
Indexの種類
patorash
1
830
Start-SQLの紹介
patorash
0
780
RailsアプリにGraphQLを導入してみた話
patorash
1
700
Other Decks in Programming
See All in Programming
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
140
これからAgentCoreを触る方へトレンドはGatewayです
har1101
6
500
音楽のための関数型プログラミング言語mimiumにおける多段階計算の活用
tomoyanonymous
1
350
Foundation Models frameworkで画像分析
ryodeveloper
1
130
言語を使う側から、作る側へ。 自作 Lisp で得た新たな気づき。
andpad
0
130
任せる範囲はこう広がった / How the Scope of AI Delegation Has Expanded
nrslib
1
270
JAWS-UG横浜 #102 AWSサ終供養LT会 成仏できない AWS サービスたち 〜本日、三体供養します〜
maroon1st
0
220
初めてのKubernetes 本番運用でハマった話
oku053
0
130
GitHubCopilotCLIのスラッシュコマンドを自作してみる
htkym
0
100
ソフトウェア設計に溶けるインフラ ― AWS CDK のインフラ認識論
konokenj
2
620
What's New in Android 2026
veronikapj
0
130
Lean は証明の正しさを確認するためだけのツールって思ってませんか?
inoueasei
1
100
Featured
See All Featured
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
The agentic SEO stack - context over prompts
schlessera
0
850
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
The Art of Programming - Codeland 2020
erikaheidi
57
14k
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
210
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
190
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
590
Navigating Team Friction
lara
192
16k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3.4k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
370
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
YesSQL, Process and Tooling at Scale
rocio
174
15k
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回アクセスしたときの結果をキャッシュして再利用する 頻繁に更新されないもの、且つ頻繁に参照されるものに関して有用(トップページとか) データを更新したときにキャッシュを削除し忘れると事故になるので注意
以上