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
Transcript
8 slides
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回アクセスしたときの結果をキャッシュして再利用する 頻繁に更新されないもの、且つ頻繁に参照されるものに関して有用(トップページとか) データを更新したときにキャッシュを削除し忘れると事故になるので注意
以上
More Decks by patorash
See All by patorash
中間管理職はそこそこ楽しい
patorash
0
69
情報共有戦略と戦術
patorash
1
1.4k
exists?で起きるN+1問題にSetで対処する
patorash
0
890
DBのメタデータを管理する文化を作る
patorash
0
730
Stimulusのススメ
patorash
0
110
わかった気になる!OpenID Connect
patorash
2
2.2k
Indexの種類
patorash
1
860
Start-SQLの紹介
patorash
0
800
RailsアプリにGraphQLを導入してみた話
patorash
1
720
Other Decks in Programming
See All in Programming
baseline으로 마이그레이션, 리팩토링 안전하게 맡기기
fornewid
1
980
Kawaiicon: Kubernetes, it's no longer cool, but let's secure it anyway!
barnbarn
0
130
AI-Powered Domain-Driven Design
ziobrando
0
440
小さなアプリを作って公開するまで
onozaty
0
170
SFCで実現する病院・医療システムの動的組み立て式UI
koukimiura
1
180
當我們開始不讀 Code - AI Agent 時代,Code Review 與 Clean Code 還剩下什麼?
jame2408
1
370
Secure Your Coding Agent Like It’s Malware (v1.5) 🇬🇧 @Code.talks 2026
richargh
0
140
Engineering Biometric UX Across Android Versions | DroidKnights 2026
ryunen344
1
200
ビジネスロジックを堅牢に~Scala 3のドメインプリミティブ~
j5ik2o
0
100
あなたの await、OS まで届いてる? strace で覗く JavaScript の非同期
hasutosasaki
1
2.3k
Web Framework Benchmark Battle 2026, Any%
a_matsuda
3
3.2k
AI駆動開発4年目の現在地(『あなたはFDEになるべきか?』イベント資料)
kumaiu
0
610
Featured
See All Featured
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
2
520
Why You Should Never Use an ORM
jnunemaker
PRO
61
10k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
750
The browser strikes back
jonoalderson
0
1.9k
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
460
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
1
300
Design in an AI World
tapps
1
390
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
1
1.7k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
750
GitHub's CSS Performance
jonrohan
1033
470k
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
2
980