Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
FARTS - Faster ActiveRecord Through SQL
Search
Kyle Balderson
February 05, 2016
Programming
0
77
FARTS - Faster ActiveRecord Through SQL
Exploring ways of filtering, calculating, and sorting using SQL instead of Iterating over records
Kyle Balderson
February 05, 2016
Tweet
Share
Other Decks in Programming
See All in Programming
ぼくの開発環境2026
yuzneri
0
210
AIエージェント、”どう作るか”で差は出るか? / AI Agents: Does the "How" Make a Difference?
rkaga
4
2k
Apache Iceberg V3 and migration to V3
tomtanaka
0
160
フロントエンド開発の勘所 -複数事業を経験して見えた判断軸の違い-
heimusu
7
2.8k
IFSによる形状設計/デモシーンの魅力 @ 慶應大学SFC
gam0022
1
300
OCaml 5でモダンな並列プログラミングを Enjoyしよう!
haochenx
0
140
CSC307 Lecture 03
javiergs
PRO
1
490
AtCoder Conference 2025
shindannin
0
1.1k
AI Schema Enrichment for your Oracle AI Database
thatjeffsmith
0
280
humanlayerのブログから学ぶ、良いCLAUDE.mdの書き方
tsukamoto1783
0
190
MDN Web Docs に日本語翻訳でコントリビュート
ohmori_yusuke
0
650
今こそ知るべき耐量子計算機暗号(PQC)入門 / PQC: What You Need to Know Now
mackey0225
3
370
Featured
See All Featured
How to Ace a Technical Interview
jacobian
281
24k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3.2k
Done Done
chrislema
186
16k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1k
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
66
A Tale of Four Properties
chriscoyier
162
24k
Building AI with AI
inesmontani
PRO
1
690
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
77
Docker and Python
trallard
47
3.7k
Heart Work Chapter 1 - Part 1
lfama
PRO
5
35k
Into the Great Unknown - MozCon
thekraken
40
2.3k
Accessibility Awareness
sabderemane
0
51
Transcript
FASTER ACTIVE RECORD THROUGH SQL FA R T S @kylebalderson
WHY SHOULD WE FARTS? THERE ARE DIFFERENT WAYS OF GETTING
SIMILAR RESULTS
A PROBLEM IT’S TOO SLOW
13,668 TOTAL ORDERS 46.29 SECONDS 43 RECENTLY SHIPPED HOW SLOW?
WHAT’S THE CAUSE? def recently_shipped_orders @shipped_orders = Order.all.select do |o|
o.current_state == “shipped” end @recently_shipped_orders = @shipped_orders.select do |o| o.updated_at <= 3.days.ago end end
THIS LOADS EVERY ORDER IN THE DATABASE INTO RUBY OBJECTS
AND LOOPS THROUGH THEM TWICE
EVERY SINGLE ORDER def recently_shipped_orders @shipped_orders = Order.all.select do |o|
o.current_state == “shipped” end @recently_shipped_orders = @shipped_orders.select do |o| o.updated_at <= 3.days.ago end end
A FASTER WAY def recently_shipped_orders @shipped_orders = Order.where(current_state: "shipped") @recently_shipped_orders
= @shipped_orders.where(“updated_at > ?", 3.days.ago) end SELECT “orders”.* FROM “orders” WHERE “orders”.“current_state” = ‘shipped’ AND (updated_at > ‘2016-02-01’)
HOW MUCH FASTER? 46.29 SECONDS 0.02 SECONDS
WHAT ELSE CAN WE DO WITH SQL?
TOTALING ATTRIBUTES ON MODELS LineItem .where("created_at > ?", 1.month.ago) .reduce(0){
|total, line_item| total + line_item.quantity } LineItem .where("created_at > ?", 1.month.ago) .sum(:quantity) 26.56 SECONDS 0.061 SECONDS
OTHER MATH FUNCTIONS ▸ average ▸ count ▸ maximum ▸
minimum ▸ sum
WHEN TO AVOID COLLECT / MAP
GET AN ARRAY OF ATTRIBUTES LineItem.all. collect do |line_item| line_item.item_color
end LineItem .where("created_at > ?", 1.month.ago) .pluck(:item_color) 1.14 SECONDS 0.08 SECONDS
RELATED RECORDS Order .where("created_at > ?", 6.months.ago) .joins(:line_items) Order .where("created_at
> ?", 6.months.ago) .collect do |order| order.line_items.any? end 7.70 SECONDS 0.01 SECONDS
NO RELATED RECORDS Order .where("created_at > ?", 6.months.ago) .joins(" LEFT
JOIN line_items ON line_items.order_id = orders.id” ) .group("orders.id") .having("COUNT(line_items.*) = ?", 0) Order .where("created_at > ?", 6.months.ago) .collect do |order| order.line_items.empty? end 7.79 SECONDS 0.06 SECONDS
QUESTIONS?
THANK YOU @kylebalderson FASTER ACTIVERECORD THROUGH SQL (FARTS)