Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

farshadf's avatar

how to chunk a join query in laravel

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.

0 likes
10 replies
farshadf's avatar

@tray2 because i am using spatie query builder and its a costum sort i must not return an instance of query builder elequeent so i cant use paginate or get .

farshadf's avatar

right but as long as its a custom sort it cant be used paginate on that and i use the paginate on my main query and yet this join takes 10 to 30 secs

jlrdw's avatar

Maybe some refactoring is in order because joins and group by's has to transverse the whole table, so if this is a huge data set that could be the problem.

Is there a way to narrow the results.

Also make sure any search fields are indexed.

farshadf's avatar

no unfortunately there is no way its a sort and it should use all data to sort the collection

farshadf's avatar

@jlrdw what is the best practice for doing that is there any good resource for that ?

jlrdw's avatar
jlrdw
Best Answer
Level 75

In a mysql front end, I use sqlyog, but I imagine phpmyadmin would work. You just define an index on those columns.

But you could also not join and just double paginate,

Have parent at top and child records underneath. A little work involved however for double pagination.

This is standard for things like accounts receivable reports.

parent company
----- child records looped over here with pagination

Eloquent has eager loading that might work, look over docs on eoquent relations.

Please or to participate in this conversation.