It seems that eloquent uses a single query for "with" regardless of how many ids there are
Book::with('author')->get();
This would trigger those two queries:
SELECT * FROM books;
SELECT * FROM authors WHERE id IN (...);
The second query may have thousands of author ids in the where clause which might cause problems with performance.
Is there some way so it would chunk that when using with?
I am aware that it is generally not a good idea to query such big result sets.
2
Answers
Yes there are, and its clear in the documentation, you can do something like this:
Or you can also do this:
You can get the details in the documentation to learn more:
https://laravel.com/docs/9.x/eloquent#chunking-results
if you need only Books and count authors you can use withCount method