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
62
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
7日分も開けないレポートが180日分返るまで - 計測と修正のサイクルで回すActiveRecordパフォーマンス改善
hirocy
October 29, 2026
Transcript
15 slides
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 もう一度計測
More Decks by hirocy
See All by hirocy
connect-goとsqlboilerで構築するクリーンアーキテクチャ
hirocy
0
2k
Other Decks in Programming
See All in Programming
AIコーディング時代のKotest ユニットテストから APIレベルの仕様固定化テストへ
ooga04
0
330
AI Augmentation Techniques with Liberty
ivargrimstad
1
570
なぜ私たちは「現時点では」KMPの導入を見送ったのか
chigichan24
1
630
Secure Your Coding Agent Like It’s Malware (v1.5) 🇬🇧 @Code.talks 2026
richargh
0
200
社内AIエージェント「Askワンバン」における コストチューニングの取り組み
t0yohei
0
180
止まらない比較を止める — 余帰納法で無限ストリームを比べる
tarao
0
210
Getting Started with Vibe Coding
miccheng
0
250
Debian JP Projectのサーバー構成
uwabami
1
180
선언형 햅틱 라이브러리 진동을 소개합니다
l2hyunwoo
1
330
Math.sumPreciseから学ぶ、ECMAScript仕様の数値の種類
syumai
0
150
複数ドメインを一つのアプリに統合するFlutterの設計と運用_FlutterKaigi2026登壇資料(須永高弘)
takahirosunaga
0
210
[DroidKnights 2026] Workflow AX: From Auto-Documented Policies to E2E Testing
raeminkang
0
610
Featured
See All Featured
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
360
31k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.2k
How to Think Like a Performance Engineer
csswizardry
28
3k
[SF Ruby Conf 2025] Rails X
palkan
3
1.6k
AI: The stuff that nobody shows you
jnunemaker
PRO
12
1.3k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.7k
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
73
43k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
360
The Limits of Empathy - UXLibs8
cassininazir
2
800
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
6
1.3k
Designing for Performance
lara
612
70k
How to Talk to Developers About Accessibility
jct
2
630