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
俺たちは雰囲気で scope をやっているけどもうちょっとなんとかならんのか?
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
tokai235
October 17, 2024
Technology
1.3k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
俺たちは雰囲気で scope をやっているけどもうちょっとなんとかならんのか?
2024/10/17 の Reject on Rails 2024 at Gotanda.rb で話した内容です。
tokai235
October 17, 2024
More Decks by tokai235
See All by tokai235
toB プロダクトから見たWAF
tokai235
0
380
チームの境界をブチ抜いていけ
tokai235
0
770
Other Decks in Technology
See All in Technology
AI コーディングと人間の認知スループット / CTO Night & Day 2026 Morning Session / AI Coding and Human Cognitive Throughput
prog893
5
1.3k
USB PDを使ってスマホを労わりたい
raspython3
0
180
AIカンパニーへの転換を支えた、エンジニアの仕事の再定義
kworkdev
PRO
2
1.3k
Kiro Crewで「みのるん式」仕事術やってみた
minorun365
PRO
3
690
大型犬好きによるPugの紹介
yamanoku
1
160
AI導入は済んだ。次は統制を作り替える番だ_AI×開発組織 Summit 2026
techtekt
PRO
0
370
AIがコードを書く時代に、エンジニアは何で自分の価値を証明するのか / In an era where AI writes code how do engineers prove their value
carta_engineering
3
1.1k
Active Record Connection Adaptersにおける長年の課題に再び取り組む
yahonda
0
200
10年後、Engineering 行為を剥奪されるとして、わたしたちは、どう、何を創ればいいだろうか。 / If Engineering Is Taken Away in 10 Years, How and What Should We Create?
bitkey
PRO
1
200
Getting Started with Google Antigravity
glaforge
0
320
会社紹介資料 / Sansan Company Profile
sansan33
PRO
31
430k
実践!Cloud Run ハンズオン!! / Cloud Run handson
y0hgi
2
300
Featured
See All Featured
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
330
How to Think Like a Performance Engineer
csswizardry
28
2.9k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.5k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
35
2.9k
[RailsConf 2023] Rails as a piece of cake
palkan
59
7.1k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
330
New Earth Scene 8
popppiees
4
2.7k
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
63
46k
Balancing Empowerment & Direction
lara
6
1.3k
Reflections from 52 weeks, 52 projects
jeffersonlam
356
22k
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
360
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
260
Transcript
俺たちは雰囲気で scope をやっているけど もうちょっとなんとかならんのか? Reject on Rails 2024 Gotanda.rb 2024-10-17
tokai235
自己紹介 • tokai235(トーカイ) です • 現実世界ではギフティで働いています ◦ 関西在住なのでこのオフィスでは「たまにいる人」です • 実は
Gotanda.rb 初参加、初登壇です ◦ そんなことある? ◦ やっていきます
Rails の scope 使ってますか?
scope とは • Rails Guidesによると ◦ よく使うクエリをスコープに設定すると、関連オブジェクトやモデルへのメソッ ド呼び出しとして参照できるようになります。 ◦ ref:
https://railsguides.jp/active_record_querying.html#スコープ こういうやつ (Rails Guides から抜粋)
もうちょっとわかりやすく • SQL のパーツを Rails で書けるくんです • 共通化や再利用ができるようになります
便利な scope ですが... こう思ったことないですか?
class method と 何が違うの?
そうなんすよ • 実は↓の2つは同じ挙動をします • シンプルな使い方をしている場合はどちらでも結果が同じ ◦ これがむずい この2つは同じ挙動
じゃあ何が違うのか(定義編) • ぶっちゃけこれに尽きます • どのスコープメソッドも、常にActiveRecord::Relationオブジェクトを返しま す。 • ref: https://railsguides.jp/active_record_querying.html#スコープ
どういうこと? • scope で返していいのは SQL のパーツだけです ◦ メソッドチェーンがつながらなくなるので ◦ Rails
Guides でも↓のように言っています ▪ スコープの本体では、別のスコープなどのメソッドをスコープ上で呼び出せ るようにするため、ActiveRecord::Relation か nil のいずれかを返すように すべきです。 ▪ ref: https://railsguides.jp/active_record_querying.html#スコープ ◦ nil は何もしない扱いになります
たとえば • こういうのはだめです • pluck はただの array を返すので、その後の with_many_comments を解釈できずに
エラーになります • pluck(:id) は class method などで追加しましょう
じゃあ何が違うのか(実装編) • scope の利点は scope で使えることです ◦ 哲学ですね • has_many
で使えたり • リレーション先のクエリで使えたり • これらは class method ではできません • データの取得を SQL に寄せることで、パフォー マンスやメモリ効率を良くすることができます ◦ 大規模データの扱いは SQL が得意です ◦ 事前の絞り込みで余計なインスタンスを作 らずに済みます
使ってよかった scope • そもそも今回のテーマは筆者がパフォーマンス改善をした中で得た知見をまとめた ものです • 何を改善したの? ◦ 使える商品の一覧を金額でソートして取得する ◦
これを SQL でなく Rails の class method や instance method で処理してま した ▪ map とか filter とか ◦ これに結構時間がかかってました
使ってよかった scope • どうやったの? ◦ map, filter などを scope に置き換
えて、SQL の時点でフィルタリング やソートがされるようにしました • どうだった? ◦ response time が 1/2 ~ 1/3 に改 善しました ▪ 特に p99th が良好 ◦ 詳細知りたい方はビール飲みながら お話しましょう
まとめ • scope は SQL のパーツを Rails で書けるくんです ◦ おかげで
SQL の共通化や再利用ができます ◦ 組み合わせると柔軟に SQL クエリが書けます • scope を使うときは SQL のパーツを返すようにしましょう ◦ メソッドチェーンがつながるように • データの取得を適切に SQL でハンドリングすることで、パフォーマンスやメモリ 効率をよくできます
scope やっていきましょう💪