Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
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
Spinner 軸ズレ現象を調べたらレンダリング深淵に飲まれた #レバテックMeetup
bengo4com
0
190
Navigating Dependency Injection with Metro
l2hyunwoo
1
190
Denoのセキュリティに関する仕組みの紹介 (toranoana.deno #23)
uki00a
0
170
これならできる!個人開発のすゝめ
tinykitten
PRO
0
130
AI前提で考えるiOSアプリのモダナイズ設計
yuukiw00w
0
190
tparseでgo testの出力を見やすくする
utgwkk
2
290
ゆくKotlin くるRust
exoego
1
160
リリース時」テストから「デイリー実行」へ!開発マネージャが取り組んだ、レガシー自動テストのモダン化戦略
goataka
0
140
AIコーディングエージェント(NotebookLM)
kondai24
0
240
Tinkerbellから学ぶ、Podで DHCPをリッスンする手法
tomokon
0
140
PC-6001でPSG曲を鳴らすまでを全部NetBSD上の Makefile に押し込んでみた / osc2025hiroshima
tsutsui
0
190
愛される翻訳の秘訣
kishikawakatsumi
3
350
Featured
See All Featured
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.1k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.4k
My Coaching Mixtape
mlcsv
0
13
Color Theory Basics | Prateek | Gurzu
gurzu
0
150
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
1.7k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
200
Scaling GitHub
holman
464
140k
Mobile First: as difficult as doing things right
swwweet
225
10k
The Spectacular Lies of Maps
axbom
PRO
1
400
Designing for Performance
lara
610
69k
Building the Perfect Custom Keyboard
takai
1
660
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)