我的页面需要 3-5 分钟才能加载,我怎样才能减少它?

My page takes 3-5 whole minutes to load, how can I possibly reduce it?

我目前正在处理这个项目,主页需要将近 5 分钟才能加载。我一直在努力减少 运行 时间,但我对编程还是个新手,对 pthreads 不太熟悉,但有人告诉我 pthreads 不适用于此。有人能帮我弄清楚如何更快地 运行 这段代码并阻止 Chrome 杀死处理器吗?

另外我知道我的 PHP 代码有点乱,正计划稍后清理它,但是 PHP 代码的小部分在第 0、270、420、560 行附近,等整个页面。

感谢您的宝贵时间。

这是代码片段,但您可以在下面链接的 pastebin 中找到完整代码:

<?php $k = 0; ?>

<tr>
    @foreach($mondays as $monday)
    <?php $shift_worker_id = DB::table('schedule')->where('id', $monday->id)->value('shift_worker_id') ?>
    <?php $zone_id = DB::table('schedule')->where('id', $monday->id)->value('zone_id') ?>
    <?php $zone = DB::table('zone')->where('id', $zone_id)->value('name') ?>
    <?php $worker_id = DB::table('shift_worker')->where('id', $shift_worker_id)->value('worker_id') ?>
    <?php $shift_id = DB::table('shift_worker')->where('id', $shift_worker_id)->value('shift_id') ?>
    <?php $time = DB::table('shift')->where('id', $shift_id)->value('time') ?>
    <?php $type = DB::table('shift')->where('id', $shift_id)->value('type') ?>
    <?php $name = DB::table('worker')->where('id', $worker_id)->value('name') ?>
    @if($type == "Graveyard")
        <th style="font-size:11px" colspan="1">{{$name}}</th>
        <th style="font-size:7px" colspan="1">{{$time}}</th>
        <th style="font-size:11px" colspan="1">{{$zone}}</th>
        <th colspan="1"></th>
        <th colspan="1"></th>
        <th colspan="1"></th>
        <?php $k++; ?>
    @endif
    @if($k % 4 == 0)
        </tr>
        <tr>
    @endif
    @endforeach
</tr>

https://pastebin.com/uUGrs1x0

肯定是因为N+1的问题。了解如何使用预先加载。

您正在对循环内的数据库创建大量冗余查询,可能有数千个,而不仅仅是 6 个。

Eloquent can "eager load" relationships at the time you query the parent model. Eager loading alleviates the N + 1 query problem.

https://laravel.com/docs/5.5/eloquent-relationships#eager-loading