Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

What is N+1 Query: Typical Example, Debugbar and Eager Loading

Lesson 02/21 3:43
Autoplay

Comments & Discussion

CC
Chris C ✓ Link copied!

So we came from 31 queries done in 11.6 ms to 2 queries done in 23.53 ms? Can you please clarify this? Greetings Povilas, thanks for all the work you do!

PK
Povilas Korop ✓ Link copied!

Hm, interesting catch, never really looked from that angle, I went for minimizing the queries. Perhaps for more realistic scenarios, for bigger databases, the time for those 31 queries would be much longer.

MP
Mariano Pereyra ✓ Link copied!

So I was really curious about this I had to try.

With the 10 users and 3 post per user as it is set up in the repo seeder, i have 9ms with eager load and 22ms with n+1 queries.

With 1000 users, 25-37ms with eager vs 1.8secs with n+1

Also went a little bit extreme and try with 10000 users (had to boost the memory_limit from php.ini), with eager loading I had 234ms and with n+1 queries actually creash with "Maximum execution time of 60 seconds exceeded" error.

It was a fun test, I don't know why you had reversed results.

LN
Loganathan Natarajan ✓ Link copied!

Thanks for the simple words of explanation.

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.