【问题标题】:How to speed up the search function of datatables?如何加快数据表的搜索功能?
【发布时间】:2015-11-10 02:41:31
【问题描述】:

这是我在表(数据表)中搜索的代码: 总记录:333,213 搜索/显示结果的预计时间:5 到 10 秒。

using :  ajax: "sample.php", // json datasource

如何快速? 我应该如何修复数据库或我正在使用的代码。

<?php
/* Database connection start */
include ('connectvl.php');
/* Database connection end */

// storing  request (ie, get/post) global array to a variable  
$requestData= $_REQUEST;


$columns = array( 
// datatable column index  => database column name
    0=> 'id',
    1=> 'FULLNAME',
    2=> 'BrgyName',
    3=> 'BDAY',
   4=> 'RESSTREET'


);

// getting total number records without any search
$sql = "SELECT id";
$sql.=" FROM voterslist2012";
$query=mysqli_query($conn, $sql) or die("cswd_listofpendingprocessgrid.php: get employees");
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;  // when there is no search parameter then total number rows = total number filtered rows.


$sql = "SELECT id, FULLNAME, BrgyName, BDAY, RESSTREET";
$sql.=" FROM voterslist2012 WHERE 1=1";
if( !empty($requestData['search']['value']) ) {   // if there is a search parameter, $requestData['search']['value'] contains search parameter
    $sql.=" AND ( id LIKE '".$requestData['search']['value']."%' ";   
  $sql.=" OR FULLNAME LIKE '".$requestData['search']['value']."%' "; 
  $sql.=" OR BDAY LIKE '".$requestData['search']['value']."%' "; 
  $sql.=" OR BrgyName LIKE '".$requestData['search']['value']."%' )";

}

    // If there is a search parameter
/*if( !empty($requestData['search']['value']) ) {   
    $search = mysqli_real_escape_string(
       $conn,
       // Match beginning of word boundary
       "[[:<:]]".
       // Replace space characters with regular expression
       // to match one or more space characters in the target field
       implode("[[.space.]]+",             
          preg_split("/\s+/", 
             // Quote regular expression characters
             preg_quote(trim($requestData['search']['value']))
          )
       ).
       // Match end of word boundary
       "[[:>:]]"
    );


    $sql.=" AND ( id REGEXP '$search' ";    
    $sql.=" OR FULLNAME REGEXP '$search' ";
    $sql.=" OR BrgyName REGEXP '$search' ";
     $sql.=" OR BDAY REGEXP '$search' ";
    $sql.=" OR RESSTREET REGEXP '$search' )";
}*/

$query=mysqli_query($conn, $sql) or die("cswd_listofpendingprocessgrid.php: get employees");
$totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result. 
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
/* $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc  */    
$query=mysqli_query($conn, $sql) or die("cswd_listofpendingprocessgrid.php: get employees");
$data = array();
while( $row=mysqli_fetch_array($query) ) {  // preparing an array
    $nestedData=array(); 
   $nestedData[] = $row["id"];
    $nestedData[] = $row["FULLNAME"];
    $nestedData[] = $row["BDAY"];
    $nestedData[] = $row["BrgyName"];
  $nestedData[] = $row["RESSTREET"];
   $data[] = $nestedData;
}
$json_data = array(
            "draw"            => intval( $requestData['draw'] ),   // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw. 
            "recordsTotal"    => intval( $totalData ),  // total number of records
            "recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
            "data"            => $data   // total data array
            );

echo json_encode($json_data);  // send data as json format

?>

【问题讨论】:

    标签: mysqli datatables


    【解决方案1】:

    在您的WHERE 子句中使用LIKE 总是会非常缓慢,因为它会绕过所有存在的索引并使用全表扫描。我将把准备好的语句与动态 SQL 的问题留给其他人提出(您应该使用准备好的语句。;)),但是如果您将 id 设为您的主键,然后执行 exact 匹配你的WHERE 子句,差异会让你大吃一惊。

    编辑:如果可行,您应该考虑在您经常搜索的任何字段上放置索引。您的BDAY 专栏将是一个不错的选择。我对 MySQL 的优化器不够熟悉,无法判断索引 FULLNAMEBRGYNAME 是否是一个好主意,但您可以进行试验。

    【讨论】:

    • 不一定。如果通配符位于搜索字符串的末尾(在本例中就是这样),那么它应该仍然能够利用该列的索引。
    • @Lock: a) 好点,b) 因为WHERE 连词是OR,如果索引扫描不匹配,我们仍然需要进行全表扫描.
    • 意思是先生减少或删除列(brgyname)搜索将有助于优化搜索功能?
    • 另一个好点。如果您没有针对特定列的搜索条件,则在 WHERE 子句中根本不应该提及它。这将需要更多的 PHP 代码来完成,但应该会提高性能并降低搜索中出现意外行为的风险。
    • @DarwinvonCorax 谢谢。我会减少场地,让我们看到减少时间的希望。
    猜你喜欢
    • 2018-10-02
    • 2014-08-03
    • 2017-01-05
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    • 2016-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多