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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
PHPのバージョンアップ時にも役立ったAST(2026年版)
matsuo_atsushi
0
260
ローカルで稼働するAI エージェントを超えて / beyond-local-ai-agents
gawa
0
160
今年もTECHSCOREブログを書き続けます!
hiraoku101
0
180
Claude Codeログ基盤の構築
giginet
PRO
7
3.7k
Codex の「自走力」を高める
yorifuji
0
1.3k
Ruby and LLM Ecosystem 2nd
koic
1
1.3k
おれのAgentic Coding 2026/03
tsukasagr
1
120
ベクトル検索のフィルタを用いた機械学習モデルとの統合 / python-meetup-fukuoka-06-vector-attr
monochromegane
2
560
How to stabilize UI tests using XCTest
akkeylab
0
140
Symfony + NelmioApiDocBundle を使った スキーマ駆動開発 / Schema Driven Development with NelmioApiDocBundle
okashoi
0
240
Feature Toggle は捨てやすく使おう
gennei
0
370
Nuxt Server Components
wattanx
0
150
Featured
See All Featured
Evolving SEO for Evolving Search Engines
ryanjones
0
170
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3.3k
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
150
Building an army of robots
kneath
306
46k
Navigating Team Friction
lara
192
16k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
240
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Building the Perfect Custom Keyboard
takai
2
720
VelocityConf: Rendering Performance Case Studies
addyosmani
333
24k
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
190
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
68
38k
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)