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
Vueのバリデーション、結局どれを選べばいい? ― 自作バリデーションの限界と、脱却までの道のり ― / Which Vue Validation Library Should We Really Use? The Limits of Self-Made Validation and How I Finally Moved On
neginasu
2
1.6k
Ktorで簡単AIアプリケーション
tsukakei
0
110
はじめてのDSPy - 言語モデルを『プロンプト』ではなく『プログラミング』するための仕組み
masahiro_nishimi
4
16k
Goで実践するドメイン駆動開発 AIと歩み始めた新規プロダクト開発の現在地
imkaoru
4
910
What Spring Developers Should Know About Jakarta EE
ivargrimstad
0
520
Temporal Knowledge Graphで作る! 時間変化するナレッジを扱うAI Agentの世界
po3rin
1
150
チームの境界をブチ抜いていけ
tokai235
0
230
CSC305 Lecture 11
javiergs
PRO
0
280
O Que É e Como Funciona o PHP-FPM?
marcelgsantos
0
210
Reactive Thinking with Signals and the Resource API
manfredsteyer
PRO
0
110
Server Side Kotlin Meetup vol.16: 内部動作を理解して ハイパフォーマンスなサーバサイド Kotlin アプリケーションを書こう
ternbusty
3
260
バッチ処理を「状態の記録」から「事実の記録」へ
panda728
PRO
0
190
Featured
See All Featured
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.2k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
10
620
Producing Creativity
orderedlist
PRO
347
40k
The Art of Programming - Codeland 2020
erikaheidi
56
14k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
46
7.7k
Raft: Consensus for Rubyists
vanstee
140
7.2k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3.1k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
658
61k
Gamification - CAS2011
davidbonilla
81
5.5k
YesSQL, Process and Tooling at Scale
rocio
173
15k
Navigating Team Friction
lara
190
15k
Reflections from 52 weeks, 52 projects
jeffersonlam
354
21k
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)