Courses

Better Eloquent Performance

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

avatar

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!

avatar

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.

avatar

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.

avatar
Loganathan Natarajan

Thanks for the simple words of explanation.