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

kankai's avatar

Use chunk to get query still slow

Hi guys,

I building a system that need to check a records, it's output around 5,000 rows. When i use dd($data) it trace out very fast in 200ms, but when goes to view it took around 10seconds to finish loaded. I try to use chunk to get query, it had improve loaded out speed but still slow.

I try to load only {{ $row-id }} per row it become more fast, it there any problem with my code? Will it be frontend problem to cause it loading slow?

controller

$data = oldwork::where('category',$cat)->where('status',0)->where('done_time','>=', $dateFrom)->where('done_time','<=', $dateTo)->get();

$data = $data->chunk(400);

views

@foreach($data as $chunk)
                                                @foreach($chunk as $row)
                                                    <tr>
                                                        <td scope="col">
                                                            <!-- {{ $row->id }} -->
                                                        </td>
                                                        <td scope="col">
                                                            {{ $row->game_id }}
                                                        </td>
                                                        <td scope="col">
                                                            {{ $row->reference }}
                                                        </td>
                                                        <td scope="col">
                                                            {{ $row->bank }}
                                                        </td>
                                                        <td scope="col">
                                                            {{ $row->capital }}
                                                        </td>
                                                        <td scope="col">
                                                            {{ $row->done_time }}
                                                        </td>
                                                    </tr>
                                                @endforeach
                                            @endforeach
                                                
0 likes
3 replies
Sergiu17's avatar

You still running a query which returns all 5k records..limit your query

oldwork::where()->where()->limit(400)->get();

kankai's avatar

If use limit(400) only will get 400 rows? If i need to check all records in selected month it will be more than 5,000

Snapey's avatar

not sure what you think chunk is doing?

The code you show won't be the cause of the slowness

You list attributes of game_id, reference, bank capital, done_time

which of these is performing additional database queries?

Also, have you used $with in your model?

Please or to participate in this conversation.