我有一个SQL查询,可以在不到一秒的时间内返回一个大型数据集。我需要做的是修复我的PHP Laravel代码,该代码需要几秒钟才能解析并显示数据。它在一个表中显示它然后我使用DataTables对它进行分页,因为我需要能够搜索所有这些。
这是我的Laravel Blade代码:
Route::get(['uri1','uri2'], function(){ /* these two uri's do the same thing */ });
我该怎么做才能加快速度呢?
SQL查询是2.4M所以我在这里制作了一个脚本,其中包含以下代码:
<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>
这有助于调试吗?
答案 0 :(得分:1)
当您要显示大量数据时,回显数据的所有行是一个糟糕的设计。最好只回显数据块。 Datatables提供了一种有用的方法:
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"
]]
}
Datatables还提供了有关如何通过将搜索参数传递到php脚本来过滤数据的示例,以便您可以选择要返回的内容。如果你在谷歌搜索datatables server-side processing example
,你会发现更完整的例子。
Laravel / Eloquent简单示例(未完成)
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,
]