我正在使用Datatables server side processing使用 PHP、Jquery、Ajax 和 SQL Server 中的数据库。
数据在表格中正确显示,但分页和搜索等功能非常慢。每次我在搜索框中输入内容或将分页更改为下一页时,尽管我不处理大数据,但我都会等待 40 秒以上。
这是我的索引页
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset=utf-8" />
<title> Datatables using PHP Ajax Jquery </title>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.16/css/dataTables.bootstrap.min.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.16/js/dataTables.bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="table-responsive">
<table id="example" class="table table-striped table-bordered" style="width:100%">
<thead>
<tr>
<th>Country</th>
<th>Customer</th>
<th>Address</th>
<th>Contact</th>
<th>Price</th>
<th>Qty</th>
</tr>
</thead>
</table>
</div>
</div>
</body>
</html>
<script type="text/javascript" language="javascript" >
$(document).ready(function() {
$('#example').DataTable({
"columns": [
{"data": "Country"},
{"data": "Customer"},
{"data": "Address"},
{"data": "Contact"},
{"data": "Price"},
{"data": "Qty"}
],
"processing": true,
"serverSide": true,
"ajax": {
url: 'fetch.php',
type: 'POST'
}
});
} );
</script>
这是我的 fetch.php,其中是 Ajax 调用
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
if (!empty($_POST) ) {
$ser="****";
$db="****";
$user="****";
$pass="****";
$MyTable="****";
$dbDB = new PDO("odbc:Driver=ODBC Driver 13 for SQL Server;Server=****;Database=****", $user, $pass);
function getData($sql){
global $dbDB ;
global $MyTable ;
$result = $dbDB->query($sql);
$rows = $result->fetchAll(PDO::FETCH_ASSOC);
$data = array();
foreach ($rows as $row) {
$data[] = $row ; }
return $data; }
$draw = $_POST["draw"];
$orderByColumnIndex = $_POST['order'][0]['column'];
$orderBy = $_POST['columns'][$orderByColumnIndex]['data'];
$orderType = $_POST['order'][0]['dir'];
$start = $_POST["start"];
$length = $_POST['length'];
$recordsTotal = count(getData("SELECT * FROM ".$MyTable));
if(!empty($_POST['search']['value'])){
for($i=0 ; $i<count($_POST['columns']);$i++){
$column = $_POST['columns'][$i]['data'];
$where[]="$column like '%".$_POST['search']['value']."%'";
}
$where = "WHERE ".implode(" OR " , $where);
$sql = sprintf("SELECT * FROM %s %s", $MyTable , $where);
$recordsFiltered = count(getData($sql));
$sql = sprintf("SELECT Country,Customer,Address,Contact,Price,Qty FROM %s %s ORDER BY %s %s OFFSET %d ROWS FETCH NEXT %d ROWS ONLY", $MyTable , $where ,$orderBy, $orderType ,$start,$length);
$data = getData($sql);
}
else {
$sql = sprintf("SELECT Country,Customer,Address,Contact,Price,Qty FROM %s ORDER BY %s %s OFFSET %d ROWS FETCH NEXT %d ROWS ONLY", $MyTable ,$orderBy, $orderType ,$start,$length);
$data = getData($sql);
$recordsFiltered = $recordsTotal;
}
$response = array(
"draw" => intval($draw),
"recordsTotal" => $recordsTotal,
"recordsFiltered" => $recordsFiltered,
"data" => $data
);
echo json_encode($response);
}
else {
echo "NO POST Query from DataTable";
}
?>
我不明白为什么当我使用服务器端处理时搜索和分页非常慢。我检查了数据表文档中的一些选项,但它并没有加快加载时间。
有什么想法,请问问题出在哪里?非常感谢。
最佳答案
这只是猜测(即可能有一些奇怪的 JavaScript 库会影响你的加载时间),但它可能与这些行有关:
$recordsTotal = count(getData("SELECT * FROM ".$MyTable));
$recordsFiltered = count(getData($sql));
您将从数据库中选择所有内容,然后继续使用 PHP 对这些行进行计数。数据库可以使用COUNT以无限快的速度完成此操作。功能。
此外,代码的最佳路径涉及 2 个数据库查询。另一个需要 3 个查询,其中 2 个对性能的影响非常大。
关于javascript - 数据表服务器端处理速度非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50353029/