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
7日分も開けないレポートが180日分返るまで - 計測と修正のサイクルで回すActiveRec...
Search
hirocy
October 29, 2026
Programming
4
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
7日分も開けないレポートが180日分返るまで - 計測と修正のサイクルで回すActiveRecordパフォーマンス改善
hirocy
October 29, 2026
More Decks by hirocy
See All by hirocy
connect-goとsqlboilerで構築するクリーンアーキテクチャ
hirocy
0
2k
Other Decks in Programming
See All in Programming
Herb in Rails 8.2: Your ERB views, now HTML-aware @ Kaigi on Rails 2026, Tokyo, Japan
marcoroth
2
350
GemmaをJevのように使ってみる / Use Gemma like Jev
kishida
5
850
月間100万人のサービス刷新で、 技術選定をHonoに振り切った話
yutobara
8
7k
エージェントを使い続けるためのメタワーク / Meta-work for keeping using the agent
rkaga
6
1.9k
[2026-10-09]一枚の布で作らなくてもいい。 信頼性は「パッチワーク」で紡がれる~既存の運用と新しい可能性をつなぎ合わせてきたCREの話~
tosite
0
110
SFCで実現する病院・医療システムの動的組み立て式UI
koukimiura
1
130
graphql-ruby を Spinel で速くしようとした話
mtsmfm
0
120
Vue.jsで作る空間解析アプリとCapacitorプラグインのData Bridge
hal_spidernight
1
110
【加筆修正版】Laravel のアプリケーションをどこにデプロイするか #phpcon_ehime
akase244
0
160
Vue Fes Japan の “作る側”に関わってみて
naokihaba
1
190
Kotlinize Is Not Just Converting to Kotlin
chigichan24
1
200
golang-bob.pdf
iwatsukayura
0
140
Featured
See All Featured
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
63
47k
Building the Perfect Custom Keyboard
takai
2
920
HDC tutorial
michielstock
2
980
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
610
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
1
290
Documentation Writing (for coders)
carmenintech
77
5.6k
Design in an AI World
tapps
1
370
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.5k
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
580
Rails Girls Zürich Keynote
gr2m
96
14k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
330
WCS-LA-2024
lcolladotor
0
920
Transcript
7日分も開けないレポートが 180日分返るまで 計測と修正のサイクルで回す ActiveRecord パフォーマンス改善 pixiv Inc. hirocy 2026.10.19
自己紹介 💼 23新卒/広告開発 ❤ Game/VTuber hirocy 広告なんでも Unit エンジニア https://x.com/hir0cy
pixiv Ads とは... 「pixiv」の運用型広告配信プラットフォーム https://ads.pixiv.net/lp 3
レポートは 3つのテーブルを集計する 広告主はレポートを見て、予算や配信を調整する 4 campaign_delivery_report_dailies # 日別の実績(過去日) delivery_report_hourlies # 時間別の実績(当日)
campaign_bills -> bills -> bill_months # 請求(消化金額)
7日分すら、レポートが開けない 「広告のレポートが 一切見られないのですが …」 5
計測と修正の サイクル を回す 6 スパンを見る 1 2 AI で逆引き もう一度計測
4 3 コードを直す
1リクエストで SQL が322クエリ発行されて いた 388 ms の SQL が1クエリ 7
短い SQL が244クエリ
Q1. どこが悪い? SELECT delivery_report_hourlies.* ... WHERE campaign_id IN (?) --
388 ms AI で発行元を探す campaigns_scope.includes(:delivery_report_hourlies, ...) 8
A1. 全期間を preload していた SELECT delivery_report_hourlies.* ... WHERE campaign_id IN
(?) -- report_at の条件が無い campaigns_scope.includes(:delivery_report_hourlies, ...) # includes は関連を丸ごと読む。N+1 対策で付けていた 1広告あたり 2,066行。使うのは当日の 15行だけ 9
修正1. 当日分だけ includes する # 当日分だけを読む関連 has_many :todays_delivery_report_hourlies, -> {
where(report_at: Time.current.all_day) } # 期間に今日を含むときだけ includes(:todays_delivery_report_hourlies) 時間別が必要なのは当日分だけ 10
Q2. どこが悪い? SELECT bills.* FROM bills WHERE bills.id = ?
LIMIT ? -- ×122 AI で発行元を探す fixed_bills = campaign_bills.joins(bill: :bill_month) fixed_bills.map { |cb| cb.bill.bill_month.month } 11
A2. joins は関連をロードしない fixed_bills = campaign_bills.joins(bill: :bill_month) # joins は
JOIN するだけ。bill はロードされない fixed_bills.map { |cb| cb.bill.bill_month.month } # 辿るたびに1件ずつ SELECT(bills、bill_months 各122回) joins を書いていても、 N+1 になる 12
修正2. 必要な2列だけ pluck する campaign_bills .joins(bill: :bill_month) .pluck('campaign_bills.price', 'bill_months.month') 1つの
SQL で、返したい値だけを取る 13
180日分まで返せるようになった 322 → 59 クエリ SQL のクエリ数 取得期間の上限 90日 →
180日 14
重いページもトレースをもとに直せる スパンを見る AI で逆引き コードを直す • includes は、範囲を絞らないと全件を読む • joins
は、関連をロードしない 15 もう一度計測