[Proposal] allow total records count to be set manually in paginate() method #826
Comments
If you go so far as to execute the count-query yourself, couldn't you just execute both queries and create your own instance of the LengthAwarePaginator? |
I wouldn't say executing your code count query was an extreme step. Also, working with large datasets isn't going to be nice with the LengthAwarePaginator if you have to pass the whole collection (?) to it. I think what Bryn's suggesting makes sense. I can think of many scenarios where you need the count for various things inside your controller, so it only makes sense to be able to pass it to the paginate function or you're doing the same query (if not the more inefficient query Eloquent generates) twice. |
My understanding of If that's the case, it would not seem like the correct approach - in fact, it would be even more inefficient (my result set is in the thousands). |
The LengthAwarePaginator expects the items of the current page, not all the items.
|
Ah, I see - that's really not clear from the description in the docs ($items = 'All of the items being paginated.'). Also, I don't really see what |
That sounds weird. Are you iterating your paginator ( |
Reviving this issue because my options are running out. Is there some pattern or solution in Laravel/Lumen that is used when the datasets are large? |
One possible scenario is to use the count(*) over that is available in some DBMS (at least postgresql and oracle). With this, it's possible with a single query to know the total number of rows that the WHERE clause originated, along with a LIMIT, OFFSET at your choice. |
The
paginate()
method, used by the Eloquent query and query builder (laravel docs), could benefit from the ability to manually pass the total record count to it, rather than have it calculated by therunPaginationCountQuery()
method. I'd suggest as an optional (second?) parameter.Often, a query that needs to be paginated includes several joins and sub-queries that do not affect the total records, but that can add significant overhead to the time needed to run the count aggregate query - I have a real world example at the moment that is approximately 5 times slower than it needs to be.
Allowing a user to manually calculate the total records with their own simpler query, then pass to the
paginate()
method seems logical to me.The text was updated successfully, but these errors were encountered: