我的页面需要3-5分钟才能加载,我怎么可能减少它? [英] My page takes 3-5 whole minutes to load, how can I possibly reduce it?

查看:132
本文介绍了我的页面需要3-5分钟才能加载,我怎么可能减少它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

另外我知道我的PHP代码有点混乱,计划清理它稍后会出现,但在整个页面中,PHP代码的小部分位于第0,270,420,560等行。



谢谢你的时间。 / p>

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

 <?php $ k = 0; ?> 

< tr>
@foreach($ mondays as $ monday)
<?php $ shift_worker_id = DB :: table('schedule') - > where('id',$ monday-> id) - >值('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')?>> ('id',$ shift_worker_id) - > value('worker_id')?>
<?php $ shift_id = DB :: table('shift_worker') - >其中('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:11pxcolspan =1> {{$ name}}< / th>
< th style =font-size:7pxcolspan =1> {{$ time}}< / th>
< th style =font-size:11pxcolspan =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个。 b
$ b


在您查询父模型时,雄辩可以急切加载关系。急切的加载减轻了N + 1查询问题。


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


I'm currently working on this project and the main page takes nearly 5 whole minutes to load. I've been trying to reduce the runtime, but I'm still new to programming and I'm not too familiar with pthreads, but I was told pthreads wouldn't work with this. Can someone help me figure out how to run this code faster and stop Chrome from killing the processor?

Also I know my PHP code is a bit messy, was planning on cleaning it up later, but the little bits of PHP code are around lines 0, 270, 420, 560, etc throughout the entire page.

Thank you for your time.

Here's a snippet of the code, but you can find the entire code in the pastebin linked below:

<?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

解决方案

It's definitely because of N+1 problem. Learn how to use eager loading.

You're creating a lot of redundant queries to the DB inside the loop, probably thousands instead of just 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

这篇关于我的页面需要3-5分钟才能加载,我怎么可能减少它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆