簡體   English   中英

PHP加快大foreach

[英]php speed up large foreach

我有一個SQL查詢,可在不到一秒鍾的時間內返回大型數據集。 我需要做的是修復我的PHP Laravel代碼,這需要花費數秒鍾的時間來解析和顯示數據。 它將所有內容顯示在一個表中,然后使用DataTables對其進行分頁,因為我需要能夠搜索所有內容。

這是我的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>

我應該怎么做才能加快速度?

SQL查詢為2.4M,因此我在此處創建了一個腳本,其中包含以下代碼:

<?php

print str_repeat("a",24000000);

http://clashdata.tk/so.php

這有助於調試嗎?

當您要顯示大量數據時,回顯數據的所有行是一個糟糕的設計。 最好只回顯數據的一部分。 數據表提供了一種有用的方法來執行此操作:

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

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

第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"
    ]]
}

數據表還提供了有關如何通過將搜索參數傳遞給php腳本來過濾數據的示例,以便您可以選擇返回的內容。 如果您在google上搜索datatables server-side processing example ,則會找到更完整的示例。

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