3

In my search form there are 26 fields and a field called type has approximately 11960 records in mysql database table. So when I search, it's showing me :

Fatal error: Maximum execution time of 30 seconds exceeded

So that I just set following ini_set

ini_set('max_execution_time', 300);

Now It's showing the result in 241.11333703995 seconds !!!

I calculate it using this :

$f = microtime(true);
$t = microtime(true)-$f;

My question is how do I reduce this time for query results for BIG rows?

My code is something like that :

if(!empty($ad_keyword)) {
    $getSearch = "SELECT * FROM (SELECT GROUP_CONCAT(DISTINCT keywordName ORDER BY keywordName) as keywordName, ";
}
else{
    $getSearch = "SELECT ";
}

$getSearch .= " cd.cdid, cd.family_name, cd.given_name, cd.department, cd.title, company.*, users.nickname  FROM contact_details as cd LEFT JOIN users ON users.user_id = cd.user_id LEFT JOIN company ON company.cid = cd.cid LEFT JOIN contact_docs ON contact_docs.cdid = cd.cdid LEFT JOIN userkeywords ON userkeywords . cdid = cd . cdid LEFT JOIN keywords ON keywords . kid = userkeywords . kid WHERE 1=1 ";

if(!empty($ad_company)){
    $getSearch .= "AND company.company_name LIKE '$ad_company%' ";
}
if(!empty($ad_fname)){
    $getSearch .= "AND cd.family_name LIKE '$ad_fname%' ";
}
if(!empty($ad_department)){
    $getSearch .= "AND cd.department LIKE '$ad_department%' ";
}

if(!empty($ad_mp)){
    $getSearch .= "AND cd.mp >= '$ad_mp' "; 
}
if(!empty($ad_e2)){
    $getSearch .= "AND cd.e2 >= '$ad_e2' "; 
}
if(!empty($ad_pl)){
    $getSearch .= "AND cd.pl >= '$ad_pl' "; 
}
if(!empty($ad_ap)){
    $getSearch .= "AND cd.ap >= '$ad_ap' "; 
}
if(!empty($ad_j2)){
    $getSearch .= "AND cd.j2 >= '$ad_j2' "; 
}

if(!empty($ad_agreater)){
    $getSearch .= "AND cd.age >= '$ad_agreater' ";  
}
if(!empty($ad_aless)){
    $getSearch .= "AND cd.age <= '$ad_aless' "; 
}
if(!empty($ad_agreater) && !empty($ad_aless)){
    $getSearch .= "AND cd.age BETWEEN '$ad_agreater' AND '$ad_aless'";  
}

if(!empty($ad_sgreater)){
    $getSearch .= "AND cd.comp >= '$ad_sgreater' "; 
}
if(!empty($ad_sless)){
    $getSearch .= "AND cd.comp <= '$ad_sless' ";    
}
if(!empty($ad_sgreater) && !empty($ad_sless)){
    $getSearch .= "AND cd.comp BETWEEN '$ad_sgreater' AND '$ad_sless'"; 
}

if(!empty($ad_noteterm)){    
    $ad_noteterm = preg_replace("/\{ASUSIBBIR\}(.+?)\s:\s(.+?)\{ASUSIBBIR\}/m", "$2", $ad_noteterm);
    $getSearch .= "AND LOCATE('$ad_noteterm', REPLACE (notesUpdate, '{ASUSIBBIR}', ' '))";
}

if(!empty($ad_cnote)){    
    $getSearch .= "AND LOCATE('$ad_cnote', cd.characterNotes)"; 
}
if(!empty($ad_twork)){    
    $getSearch .= "AND cd.work_phone LIKE '%$ad_twork%'";   
}
if(!empty($ad_tmobile)){    
    $getSearch .= "AND cd.mobile_phone like '%$ad_tmobile%'";   
}

if(!empty($ad_resume)){    
    $getSearch .= "AND LOCATE('$ad_resume', contact_docs.file_content)";    //is this the resume? yes
}

if(!empty($ad_datefrom) && empty($ad_dateto)){    
        $getSearch .= "AND cd.created_date BETWEEN '$ad_datefrom'AND '$date'";  
}

if(!empty($ad_dateto) && empty($ad_datefrom)){    
    $getSearch .= "AND cd.created_date BETWEEN date('0000-00-00') AND '$ad_dateto' "; 
}

if(!empty($ad_datefrom) && !empty($ad_dateto)){
    $getSearch .= "AND cd.created_date BETWEEN '$ad_datefrom' AND '$ad_dateto'";    
}

if(!empty($ad_type)){    
    $getSearch .= "AND cd.type = '$ad_type' ";  
}

if(!empty($ad_wemail)){
    $getSearch .= "AND cd.email LIKE '$ad_wemail%'";    
}

if(!empty($ad_pemail)){
    $getSearch .= "AND cd.email_private LIKE '$ad_pemail%'";    
}

if(!empty($ad_title)){
    $getSearch .= "AND cd.title LIKE '$ad_title%'"; 
}

if(!empty($ad_source)){
    $getSearch .= "AND cd.source LIKE '$ad_source%'";   
}

if(!empty($ad_consultant)){
    $getSearch .= "AND users.nickname LIKE '%$ad_consultant%'"; 
}

if(!empty($ad_keyword)){
    $ad_keyword_param = str_replace(",","','",$ad_keyword);
    $getSearch .= " AND keywords.keywordName IN ('$ad_keyword_param') ";
}

$getSearch .= " GROUP BY cd.user_id, cd.cid, cd.cdid ";

if(!empty($ad_keyword)){
    $ad_keyword_param = str_replace(",",",",$ad_keyword);
    $getSearch .= " ) as a WHERE keywordName LIKE '$ad_keyword_param%' ";
}

$f = microtime(true);

//$getSearch .= "ORDER BY cd.given_name ASC";
$getSearch =  mysqli_query($link, $getSearch);
$msc=microtime(true)-$f;

$num =  mysqli_num_rows($getSearch); 
shibbir ahmed
  • 1,014
  • 2
  • 18
  • 34
  • This is a MySQL question, really, so we'd need more information about your database architecture. For example, what foreign keys are set in this database? Which columns are indexed? EDIT: Also- this code is vulnerable to SQL injection. Use prepared statements instead. – Mikel Bitson Nov 09 '15 at 18:15
  • Thanks for your suggestion @MikelBitson, I will definitely use prepared statements – shibbir ahmed Nov 09 '15 at 18:17
  • Add ```print ($getSearch)``` right before you call mysqli_query(). Then run EXPLAIN on that query. What is the output of EXPLAIN? – Sasha Pachev Nov 09 '15 at 18:27

0 Answers0