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
君も Arel おじさんになって ActiveRecord のクエリを高速化しよう / Let...
Search
森井ゴンザレス
October 12, 2016
Programming
330
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
君も Arel おじさんになって ActiveRecord のクエリを高速化しよう / Let's become Uncle Arel
森井ゴンザレス
October 12, 2016
More Decks by 森井ゴンザレス
See All by 森井ゴンザレス
CI/CD がなかった会社で勝手に CI/CD を始めた話 (仮)
morygonzalez
0
210
Product Manager の Job Description
morygonzalez
3
2.9k
Rails application development in API era
morygonzalez
0
580
Lokka についての LT
morygonzalez
0
470
BitBar で快適な生活
morygonzalez
1
4k
gyowitter のご紹介
morygonzalez
0
34k
Other Decks in Programming
See All in Programming
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
450
KiroのSpecで「五目並べ」を作ってみる
satoshi256kbyte
1
330
App Storeの外へ──日本のiOSサイドローディング入門 for iOSDC Japan 2026
yuukiw00w
0
280
市販E-Readerを乗っ取れ 〜Embedded Swiftで電子ペーパーガジェットを制御する〜
trickart
0
230
TiDB Cloudのカスタムコントローラーによるオートスケール対応
takaidohigasi
0
140
Heart of Swift Concurrency
koher
0
1.1k
FreeBSDでZabbixを動かす
kenkino
0
340
JRuby: Past, Present, and Future
headius
0
220
JPUG勉強会 OSSデータベースの内部構造を理解しよう(第2回)
oga5
0
290
Ghostty + Neovimで作る 透明でカッコ良い開発環境
j341nono
0
150
[ハンズオン]AIへの指示だけで「五目並べ」を作ってみよう
satoshi256kbyte
1
320
そのリトライ、死んだコネクションを使い回していませんか ── GoのHTTPクライアントとHTTP/2を実プロダクト障害から学び直す
myus4a
0
350
Featured
See All Featured
Designing Powerful Visuals for Engaging Learning
tmiket
1
580
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
ラッコキーワード サービス紹介資料
rakko
1
5.1M
Agile that works and the tools we love
rasmusluckow
331
22k
Six Lessons from altMBA
skipperchong
29
4.5k
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.6k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
590
Designing for Timeless Needs
cassininazir
1
510
Claude Code のすすめ
schroneko
67
230k
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
2
450
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
840
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
6.3k
Transcript
܅ Arel ͓͡͞Μʹ ͳͬͯ ActiveRecord ͷΫΤϦΛߴԽ͠Α͏ ©morygonzalez Fukuoka.rb #66
ࣗݾհ • Kaizen Platform ͱ͍͏ SaaS ͷձࣾʹࡏ੶͍ͯ͠·͢ • Ϩʔϧζྺ5͘Β͍ •
͓ͬ͞Μ͚ͩͲ ActiveRecord ʢORMʣͳ͍ͱΫΤϦॻ͚ͳ͍ΏͱΓϓϩάϥϚʔͰ͢… ©morygonzalez Fukuoka.rb #66
ʔࣾͷ DB ͷεΩʔϚɺΊͬͪΌෳࡶͰ͢ ©morygonzalez Fukuoka.rb #66
Kaizen Platform ͰͬͯΔ͜ͱ • ͓٬͞ΜͷαΠτʹ JS ೖΕͯΒ͏ • A/B ςετ͢Δ
• ܭଌ͢Δ • BigQuery ʹϩάஷΊΔ • όονͰϩάΛूܭͯ͠ MySQL ʹಥͬࠐΉ • A/B ςετͷ݁ՌΛදࣔ͢Δ • etc. ©morygonzalez Fukuoka.rb #66
ݫ͍͠… ©morygonzalez Fukuoka.rb #66
Ϣʔβʔͷ֓೦͕ෳࡶ • Ϣʔβʔ • ৫ • νʔϜ • ΤʔδΣϯτ •
etc. ©morygonzalez Fukuoka.rb #66
Ϣʔβʔͷݕࡧػೳ͕ΊͬͪΌ͍… • PM ʮϢʔβʔ໊͔৫໊͔νʔϜ໊Ͱݕࡧͯ͠Ϣʔβʔͷ updated_at ΧϥϜͰ sort ͯ͠Αʯ • Θͨ͠ʮྃղͰ͢ʯ
• Tech Lead ఼ʮεϩʔΫΤϦʹͳͬͯΔΜͰ͚͢Ͳ…ʯ ©morygonzalez Fukuoka.rb #66
MySQL ͷಛੑ • WHERE ۟Ͱ͏ΧϥϜͱ ORDER BY Ͱ sort ͢Δͱ͖ʹ
͏ΧϥϜ͕ҟͳΔͱ sort ࣌ʹΠϯσοΫε͕ΘΕͳ͍ ©morygonzalez Fukuoka.rb #66
MySQL ͷಛੑ ͜͏͍͏ͷ× CREATE TABLE `users` ( `id` int(11) NOT
NULL AUTO_INCREMENT, `username` tinyint(1) DEFAULT NULL, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4; ALTER TABLE users ADD INDEX index_users_on_username(username); ALTER TABLE users ADD INDEX index_users_on_created_at(created_at); SELECT * FROM users WHERE username = 'foo' ORDER BY created_at; ※͜ͷลదʹॻ͍ͨͷͰؒҧͬͯͨΒ͢Έ·ͤΜ… ©morygonzalez Fukuoka.rb #66
MySQL ͷಛੑ ͜͏͍͏ͷ◦ CREATE TABLE `users` ( `id` int(11) NOT
NULL AUTO_INCREMENT, `username` tinyint(1) DEFAULT NULL, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4; ALTER TABLE users ADD INDEX index_users_on_username_and_created_at(username, created_at); SELECT * FROM users WHERE username = 'foo' ORDER BY created_at; ※͜ͷลదʹॻ͍ͨͷͰؒҧͬͯͨΒ͢Έ·ͤΜ… ©morygonzalez Fukuoka.rb #66
Ͳ͏ͬͯղܾ͢Δ͔ʁ 1.ΫΤϦΛ2ճʹ͚Δʂʂʂɺʂ 2.select ͢ΔΧϥϜΛݮΒ͢ ©morygonzalez Fukuoka.rb #66
ͦͷൃͳ͔ͬͨΘ… ©morygonzalez Fukuoka.rb #66
1. ΫΤϦΛ2ճʹ͚Δ • ରͷϢʔβʔΛݕࡧ͢ΔΫΤϦΛ͛ͯ user id ҰཡΛऔ Δ • ↑Ͱऔಘͨ͠
id Λ where ۟ʹೖΕͯݕࡧ͠ɺ updated_at ΧϥϜͰ sort ͢Δ • ೋͭͷΫΤϦͱΠϯσοΫεޮ͍ͯരͰ͢ ©morygonzalez Fukuoka.rb #66
2. select ͢ΔΧϥϜΛݮΒ͢ • ActiveRecord ෆඞཁͳΧϥϜ·Ͱશ෦ select ͠·͢ • ͍ͭ͜ΛΊͯ͋͛Δ͚ͩͰ
1000ms ͘Β͍͔͔ͬͯͨΫΤ Ϧ͕ 500ms ͘Β͍ʹͳΓ·͢ ©morygonzalez Fukuoka.rb #66
2. select ͢ΔΧϥϜΛݮΒ͢ User. includes(:public_organization). references(:public_organization). where(...) SELECT `users`.`id` AS
t0_r0, `users`.`username` AS t0_r1, `users`.`email` AS t0_r2, ... FROM users WHERE ... Έ͍ͨͳඇਓؒతͳΫΤϦ͕ੜ͞ΕΔ… ©morygonzalez Fukuoka.rb #66
2. select ͢ΔΧϥϜΛݮΒ͢ User. select( [@users[:id], @users[:username], @users[:allow_public_profile]] ). where(...)
SELECT users.id, users.username, users.allow_public_profile FROM users WHERE ... ৗࣝతͳΫΤϦʹͳΓ·͢ɻ ©morygonzalez Fukuoka.rb #66
ORM ͲͬΓͷΏͱΓϨΠϧβʔͩͱϝιουνΣʔϯͯ͠Ұจ ͰΫΤϦΛॻ͍ͯ͠·͍͕ͪ User.includes(:comments). where('users.name like ?', 'foo%'). where.not(foo: 'bar').
where('comments.body like ?', '%bar%') ©morygonzalez Fukuoka.rb #66
ແཧ͠ͳ͍͍ͯ͘ΜͩΑ… ©morygonzalez Fukuoka.rb #66
MySQL ͷؾ࣋ͪʹͳΖ ͏ʂʂɺʂ ©morygonzalez Fukuoka.rb #66
ORM ΛΘͣʹ SQL Λॻ͘ͱ͖ʹΈ͍ͨʹαϒΫΤϦʹͨ͠ ΓɺΫΤϦΛׂͨ͠Γͯ͠ΠϯσοΫε͕͑ΔΑ͏ͳΫΤϦΛ ࡉ͔͚ͯ͛ͯ͘σʔλΛऔಘ͠·͠ΐ͏ ©morygonzalez Fukuoka.rb #66
ԾʹϝιουνΣʔϯͯ͠ෳࡶͳΫΤϦ͕ҰߦͰॻ͚ͯɺΠϯσ οΫε͕ޮ͔ͣʹ͔ͬͨΓ MySQL ʹෛՙΛ͔͚͍ͯͨΒҙຯ ͕͋Γ·ͤΜɻ ©morygonzalez Fukuoka.rb #66
Further Reading • ArelͰ৭ΜͳSQLΛΈཱͯͯΈΔ - ryopeko ͷԿ͔ • (Φτί)ͷίϯϐϡʔλಓ: Using
filesort ©morygonzalez Fukuoka.rb #66
·ͱΊ • Rails Ͱ ActiveRecord ͔ΓͬͯΔͱΫΤϦͷνϡʔ χϯά͕͓Ζ͔ͦʹͳΔ • SQL ॻ͍ͯ
Arel ʹͯ͠Կͱ͔͠Α͏ʂʂʂɺʂ ©morygonzalez Fukuoka.rb #66