简体   繁体   English

PHP加快大foreach

[英]php speed up large foreach

I have a SQL Query that returns a large dataset in less than a second. 我有一个SQL查询,可在不到一秒钟的时间内返回大型数据集。 What I need to do is fix my PHP Laravel code which is taking serveral seconds to parse and display the data. 我需要做的是修复我的PHP Laravel代码,这需要花费数秒钟的时间来解析和显示数据。 It displays it all in one table and then I use DataTables to paginate it because I need to be able to search through all of it. 它将所有内容显示在一个表中,然后使用DataTables对其进行分页,因为我需要能够搜索所有内容。

This is my Laravel Blade code: 这是我的Laravel Blade代码:

<div class="body">


 @if(Request::url() === 'http://clashdata.tk/clans')
    <h2>2000 Oldest Active Clans</h2>
 @elseif(Request::url() === 'http://clashdata.tk/clans/orderby/level')
    <h2>2000 Clans In Order of Level</h2>
 @elseif(Request::url() === 'http://clashdata.tk/clans/orderby/score')
    <h2>2000 Clans In Order of Score</h2>
 @elseif(Request::url() === 'http://clashdata.tk/clans/orderby/warclans')
    <h2>2000 Best War Clans</h2>
 @elseif(Request::url() === 'http://clashdata.tk/clans/orderby/warswon')
    <h2>2000 Clans In Order of Wars Won</h2>
 @endif

<select onchange="this.options[this.selectedIndex].value && (window.location = this.options[this.selectedIndex].value);">
    <option value="">Order By...</option>
    <option value="http://clashdata.tk/clans/orderby/score">Clan Points</option>
    <option value="http://clashdata.tk/clans/orderby/level">Clan Level and Exp</option>
    <option value="http://clashdata.tk/clans/orderby/warclans">Best War Clans</option>
    <option value="http://clashdata.tk/clans/orderby/warswon">Wars Won</option>
    <option value="http://clashdata.tk/clans/">Time Made</option>
</select>


<br /><br />

<table cellspacing="20" id="table" border="1" class="display dataTable dtr-inline" style="border-collapse:collapse;text-align:center;">
    <thead>
        <tr>
            <td>#</td>
            <td>Search Clan Names</td>
            <td>Players</td>
            <td>Wars Won</td>
            <td>Wars Lost</td>
            <td>Wars Tied</td>
            <td>Clan Level</td>
            <td>Clan Experience</td>
            <td>Search Clan Locations</td>
            @if(Request::url() === 'http://clashdata.tk/clans/orderby/warclans')
                <td>War Rating</td>
            @else
                <td>War Win %</td>
            @endif
        </tr>
    </thead>

    <tbody>

    @foreach($clan as $currentclan)

    <tr>

        <td>{{ $currentclan->rank }}</td>
        <td><a href="/clans/{{ $currentclan->id }}">{{ $currentclan->name }}</a></td>
        <td>{{ $currentclan->playercount }}</td>
        <td>{{ $currentclan->warswon }}</td>
        <td>{{ $currentclan->warslost }}</td>
        <td>{{ $currentclan->warstied }}</td>
        <td>{{ $currentclan->level }}</td>
        <td>{{ $currentclan->exp }}</td>
        <td>{{ $currentclan->location }}</td>
        <td>{{ $currentclan->warwinpercent }}</td>
    </tr>

    @endforeach
    </tbody>

</table>

</div>

<script>

    $(document).ready( function () {

        $('#table').DataTable({
            "lengthMenu": [ 10, 25, 50, 75, 100 ],
            "autoWidth": false,
            "ordering": false,
            "pagingType": "full_numbers"
        });

        $('#table').dataTable().columnFilter({
            sPlaceHolder: "head:before",
            aoColumns: [null, { type: "text" }, null, null, null, null, null, null, { type: "text" }, null]
        });

    } );
</script>

What should I do to speed this up? 我应该怎么做才能加快速度?

The SQL Query is 2.4M so I made a script here which contains the following code: SQL查询为2.4M,因此我在此处创建了一个脚本,其中包含以下代码:

<?php

print str_repeat("a",24000000);

http://clashdata.tk/so.php http://clashdata.tk/so.php

Does that help debugging it? 这有助于调试吗?

Echoing out all rows of your data is a poor design when you have a lot of data to display. 当您要显示大量数据时,回显数据的所有行是一个糟糕的设计。 It is better to echo out only chunks of your data. 最好只回显数据的一部分。 Datatables provides a useful way to do this: 数据表提供了一种有用的方法来执行此操作:

https://www.datatables.net/examples/data_sources/server_side.html https://www.datatables.net/examples/data_sources/server_side.html

Step 1: Set up html and javascript 第1步:设置html和javascript

<table id="example" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>First name</th>
                <th>Last name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <th>First name</th>
                <th>Last name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </tfoot>
    </table>
<script>
//assuming you have included all prerequisite scripts for Datatables
$(document).ready(function() {
    $('#example').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": "url_to_your_server_processing_script.php"
    } );
} );
</script>

Step 2: write php script that will return just a chunk of your data like this: 第2步:编写php脚本,它将仅返回部分数据,如下所示:

{
  "draw": 1,
  "recordsTotal": 57,
  "recordsFiltered": 57,
  "data": [[
      "Airi",
      "Satou",
      "Accountant",
      "Tokyo",
      "28th Nov 08",
      "$162,700"
    ],
    [
      "Angelica",
      "Ramos",
      "Chief Executive Officer (CEO)",
      "London",
      "9th Oct 09",
      "$1,200,000"
    ]]
}

Datatables also gives examples on how to filter your data by passing search parameters to your php script so you can choose what to return. 数据表还提供了有关如何通过将搜索参数传递给php脚本来过滤数据的示例,以便您可以选择返回的内容。 If you search google for datatables server-side processing example , you will find more complete examples. 如果您在google上搜索datatables server-side processing example ,则会找到更完整的示例。

Laravel/Eloquent simple example (not complete) Laravel /雄辩的简单示例(不完整)

Use App\Clan

$count = Clan::count();
$clans = Clan::where('some_column','some_value')
    ->skip(20)
    ->take(10)
    ->get();

return [
    'draw' => 1,
    'recordsTotal' => $count,
    'recordsFiltered' => 10,
    'data' => $clans,
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM