SlideShare a Scribd company logo
1 of 13
N+1 Errors
Hi! I’m Esther.
I’m a software developer at MarryMapp
and a recent Dev Bootcamp grad
(and I <3 Ruby)
estherleytush.com
This talk was made with newbies in mind.
What are n+1 errors,
in a nutshell?
Here’s what happens...
Database is
queried for
parent record
App needs info
about child
records
Database is
queried once per
child record
Products and users:
×An product index page asks for info
about some products...
×Then needs to load info about the
product creator…
×...querying the database for each
creator.
So, why is this
a problem?
What if you’re making 100s, 1000s,
10000(...)’s requests?
What if your server is far away?
If it takes 1ms to make 1 request, 2ms for 2...
Those numbers compound painfully.
Because it slows pages down.
What would be faster?
Making 1 query with 100 results
instead of
100 queries with 1 result each.
Let’s go
see it!
Your Rails tools
:preload
:includes
:eager_load
Pre-make aggregate database
queries for the resource you
need
Post.preload(:user)
Associations
Pre-make an association for the
resource you’ll need in order to
preload it
has_one :latest_post,
-> { order(created_at: :desc).limit(1) },
class_name: "Post"
“Try the Bullet gem, too:
It informs you of n+1 errors via excellent
logging capability.
Thanks!
Any questions?
estherleytush.com
Github: mindplace

More Related Content

Viewers also liked

부천오피&동탄오피#사이트【http://dasom10.net】강남역오피
부천오피&동탄오피#사이트【http://dasom10.net】강남역오피부천오피&동탄오피#사이트【http://dasom10.net】강남역오피
부천오피&동탄오피#사이트【http://dasom10.net】강남역오피
dasomnet1
 
1st Buikwe District Teachers' Forum - 1st Briefing Session
1st Buikwe District Teachers' Forum - 1st Briefing Session1st Buikwe District Teachers' Forum - 1st Briefing Session
1st Buikwe District Teachers' Forum - 1st Briefing Session
lissalourenco
 

Viewers also liked (8)

부천오피&동탄오피#사이트【http://dasom10.net】강남역오피
부천오피&동탄오피#사이트【http://dasom10.net】강남역오피부천오피&동탄오피#사이트【http://dasom10.net】강남역오피
부천오피&동탄오피#사이트【http://dasom10.net】강남역오피
 
Linea de tiempo yajaira
Linea  de  tiempo   yajairaLinea  de  tiempo   yajaira
Linea de tiempo yajaira
 
Как выжить на локальном рынке с приходом федеральных игроков
Как выжить на локальном рынке с приходом федеральных игроковКак выжить на локальном рынке с приходом федеральных игроков
Как выжить на локальном рынке с приходом федеральных игроков
 
Richard Philips - Digital Transformation Programme Director, The AA
Richard Philips - Digital Transformation Programme Director, The AARichard Philips - Digital Transformation Programme Director, The AA
Richard Philips - Digital Transformation Programme Director, The AA
 
Lb
LbLb
Lb
 
The top do's and don'ts in a job search. Times have changed in a job search....
The top do's and don'ts in a job search.  Times have changed in a job search....The top do's and don'ts in a job search.  Times have changed in a job search....
The top do's and don'ts in a job search. Times have changed in a job search....
 
¿CÓMO ELABORAR UN PROYECTO DE ENSEÑANZA EN EDUCACIÓN FÍSICA?
¿CÓMO ELABORAR UN PROYECTO DE ENSEÑANZA EN EDUCACIÓN FÍSICA?¿CÓMO ELABORAR UN PROYECTO DE ENSEÑANZA EN EDUCACIÓN FÍSICA?
¿CÓMO ELABORAR UN PROYECTO DE ENSEÑANZA EN EDUCACIÓN FÍSICA?
 
1st Buikwe District Teachers' Forum - 1st Briefing Session
1st Buikwe District Teachers' Forum - 1st Briefing Session1st Buikwe District Teachers' Forum - 1st Briefing Session
1st Buikwe District Teachers' Forum - 1st Briefing Session
 

Similar to Avoiding N+1 Errors in Rails Apps

Synchronous Reads Asynchronous Writes RubyConf 2009
Synchronous Reads Asynchronous Writes RubyConf 2009Synchronous Reads Asynchronous Writes RubyConf 2009
Synchronous Reads Asynchronous Writes RubyConf 2009
pauldix
 
Speed as-a-feature
Speed as-a-featureSpeed as-a-feature
Speed as-a-feature
New Relic
 
ccna course 2
ccna course 2ccna course 2
ccna course 2
S Sridhar
 
Prophet - Beijing Perl Workshop
Prophet - Beijing Perl WorkshopProphet - Beijing Perl Workshop
Prophet - Beijing Perl Workshop
Jesse Vincent
 

Similar to Avoiding N+1 Errors in Rails Apps (20)

Die Another Day: Scaling from 0 to 4 million daily requests as a lone develop...
Die Another Day: Scaling from 0 to 4 million daily requests as a lone develop...Die Another Day: Scaling from 0 to 4 million daily requests as a lone develop...
Die Another Day: Scaling from 0 to 4 million daily requests as a lone develop...
 
Evolution of applications - from Enterprise to Cloud
Evolution of applications - from Enterprise to CloudEvolution of applications - from Enterprise to Cloud
Evolution of applications - from Enterprise to Cloud
 
Top-10-Java-Performance-Problems.pdf
Top-10-Java-Performance-Problems.pdfTop-10-Java-Performance-Problems.pdf
Top-10-Java-Performance-Problems.pdf
 
React Apps at AirHelp. Lessons learned.
React Apps at AirHelp. Lessons learned.React Apps at AirHelp. Lessons learned.
React Apps at AirHelp. Lessons learned.
 
Big data and APIs for PHP developers - SXSW 2011
Big data and APIs for PHP developers - SXSW 2011Big data and APIs for PHP developers - SXSW 2011
Big data and APIs for PHP developers - SXSW 2011
 
Reproducible datascience [with Terraform]
Reproducible datascience [with Terraform]Reproducible datascience [with Terraform]
Reproducible datascience [with Terraform]
 
GraphLab Conference 2014 Keynote - Carlos Guestrin
GraphLab Conference 2014 Keynote - Carlos GuestrinGraphLab Conference 2014 Keynote - Carlos Guestrin
GraphLab Conference 2014 Keynote - Carlos Guestrin
 
Synchronous Reads Asynchronous Writes RubyConf 2009
Synchronous Reads Asynchronous Writes RubyConf 2009Synchronous Reads Asynchronous Writes RubyConf 2009
Synchronous Reads Asynchronous Writes RubyConf 2009
 
Dato Keynote
Dato KeynoteDato Keynote
Dato Keynote
 
Speed as-a-feature
Speed as-a-featureSpeed as-a-feature
Speed as-a-feature
 
PSU Guest Lecture: Database Programming
PSU Guest Lecture: Database ProgrammingPSU Guest Lecture: Database Programming
PSU Guest Lecture: Database Programming
 
2019 StartIT - Boosting your performance with Blackfire
2019 StartIT - Boosting your performance with Blackfire2019 StartIT - Boosting your performance with Blackfire
2019 StartIT - Boosting your performance with Blackfire
 
Joyent circa 2006 (Scale with Rails)
Joyent circa 2006 (Scale with Rails)Joyent circa 2006 (Scale with Rails)
Joyent circa 2006 (Scale with Rails)
 
Top 10 Scalability Mistakes
Top 10 Scalability MistakesTop 10 Scalability Mistakes
Top 10 Scalability Mistakes
 
Towards FutureOps: Stable, Repeatable environments from Dev to Prod
Towards FutureOps: Stable, Repeatable environments from Dev to ProdTowards FutureOps: Stable, Repeatable environments from Dev to Prod
Towards FutureOps: Stable, Repeatable environments from Dev to Prod
 
Hofstra University - Overview of Big Data
Hofstra University - Overview of Big DataHofstra University - Overview of Big Data
Hofstra University - Overview of Big Data
 
ccna course 2
ccna course 2ccna course 2
ccna course 2
 
Large Components in the Rearview Mirror
Large Components in the Rearview MirrorLarge Components in the Rearview Mirror
Large Components in the Rearview Mirror
 
Prophet - Beijing Perl Workshop
Prophet - Beijing Perl WorkshopProphet - Beijing Perl Workshop
Prophet - Beijing Perl Workshop
 
Breaking the Oracle Tie; High Performance OLTP and Analytics Using MongoDB
Breaking the Oracle Tie; High Performance OLTP and Analytics Using MongoDBBreaking the Oracle Tie; High Performance OLTP and Analytics Using MongoDB
Breaking the Oracle Tie; High Performance OLTP and Analytics Using MongoDB
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Recently uploaded (20)

Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 

Avoiding N+1 Errors in Rails Apps

Editor's Notes

  1. This can become a huge problem, especially if your server is far away from the requesting client. If it takes 1ms to process a single request, think of how that gets compounded when you’re making hundreds, thousands, or more requests at the same time. This is why N+1 errors are considered one of the common reasons for slow page loads.
  2. This can become a huge problem, especially if your server is far away from the requesting client. If it takes 1ms to process a single request, think of how that gets compounded when you’re making hundreds, thousands, or more requests at the same time. This is why N+1 errors are considered one of the common reasons for slow page loads.