You are just adding limit twice. It makes absolutely no difference
Dec 11, 2022
3
Level 3
Does using limit() before pagination to optimize withSum method make sense?
I'm using ->withSum(['column1', 'column2']) method to get sum of selected columns from multiple rows in the table. I was wondering if I can use ->limit($paginate_count) before withSum() and paginate() methods to optimize the speed a little. Does this make sense?
I added it to my command and nothing much changed, or at least it didnt break 😅
I just wanted to ask if this makes sense or is it unnecessary and laravel already gets the sums after doing the pagination.
Example code :
$customers = Customer::select(['id', 'name', 'contact'])
->where('address', 'like', '%' . $search_word . '%')
->orWhere('contact', 'like', '%' . $search_word . '%')
->orWhere('name', 'like', '%' . $search_word . '%')
->orderBy('created_at', 'desc')
->limit($this->paginate_count)
->withSum('sales', 'price_paid')
->paginate($this->paginate_count)
->appends(request()->query());
Level 102
1 like
Please or to participate in this conversation.