April 3, 2021
What N+1 problem actually is?algorithm
I was reading a post on Laravel news which includes a number of tips to improve your database queries. It has tons of stuff that everyone should be aware of. Tip#5 specifies that you should be aware of N+1 problem. It explains clearly what the N+1 problem is.
So if there are N number of posts, it will make N+1 queries( 1 query to retrieve posts and N queries to retrieve the author for each post). This is commonly known as N+1 query problem.
What does this essentialy means that if you have 10 posts, and you do 1 query to retrieve that 10 posts. But there are actually 11 queries running. 1 to get 10 posts and other 10 to retreive authors of these 10 posts.
This is commonly known as N+1 problem. In Laravel it is pretty easy to overcome this problem by eager loading whatever you want to load with actual data.