如何加快数据表的搜索功能?

时间:2015-11-10 02:41:32

标签: mysqli datatables

这是我在表格(数据表)中搜索的代码: 总记录: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

?>

1 个答案:

答案 0 :(得分:0)

LIKE子句中使用WHERE总是会变慢,因为它会绕过任何存在的索引并使用全表扫描。我将把准备好的语句与动态SQL的问题留给其他人来引发(你应该使用预备语句。;)),但如果你让id成为你的主键然后做一个 <{1}}条款中的确切匹配,差异会让你感到震惊。

编辑:如果可行,您应该考虑在经常搜索的任何字段上放置索引。您的WHERE专栏将是一个很好的候选人。我对MySQL的优化器不太熟悉,不知道索引BDAYFULLNAME是个好主意,但你可以试验一下。