0
votes

Somewhere in my Laravel application, the following query is likely to return a very large results set:

$data = $query->join('accommodation_rooms', 'accommodations.id', '=', 'accommodation_rooms.accommodation_id')
    ->join('discounts', 'accommodation_rooms.id', '=', 'discounts.accommodation_room_id')
    ->select('accommodation_rooms.id')
    ->orderBy('discounts.amount', 'desc')
    ->select('discounts.amount', 'accommodations.*')
    ->groupBy('discounts.amount', 'accommodation_rooms.id');
return $data;

I wondered how I could load a part of the data faster but only load the rest later maybe using some pagination mechanism or something.

Given the data is sent from an API, I want to know how I could chunk this data.
Thank you.

1
I think you should use cursosr(), when working with big databases it works like a charmTanvir Ahmed

1 Answers

-1
votes

You can use paginate method for this purpose

$data = $query->join('accommodation_rooms', 'accommodations.id', '=', 'accommodation_rooms.accommodation_id')
->join('discounts', 'accommodation_rooms.id', '=', 'discounts.accommodation_room_id')
->select('accommodation_rooms.id')
->orderBy('discounts.amount', 'desc')
->select('discounts.amount', 'accommodations.*')
->groupBy('discounts.amount', 'accommodation_rooms.id')->paginate(15);

You can change the numbers of record to fetch in given parameter to paginate function e.g 15.