Datatables Server端处理速度很慢

时间:2018-05-15 14:39:36

标签: javascript php jquery ajax datatables

我在SQL Server中使用PHP,Jquery,Ajax和我的数据库Datatables server side processing

数据在表格中正确显示但是分页和搜索等功能非常慢。每次我输入搜索框或我将分页更改为下一页我等待超过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";
}

?>

我不明白为什么当我使用服务器端处理时搜索和分页非常慢。我检查了数据表文档中的一些选项,但它没有加快加载时间。

任何想法请问哪里可能是问题?非常感谢你。

1 个答案:

答案 0 :(得分:0)

这只是猜测(即可能有一些奇怪的javascript库正在计算你的加载时间),但它可能与这些行有关:

$recordsTotal = count(getData("SELECT * FROM ".$MyTable));

$recordsFiltered = count(getData($sql));

您正在从数据库中选择所有内容,然后继续使用PHP计算这些行。使用COUNT函数,数据库可以无限快地完成此任务。

此外,代码的最佳路径涉及2个数据库查询。另一个需要3个查询,其中2个在性能上非常重要。