php - Why mysql sql query is taking long time to get the result? -


in mysql db table contact_details have 12,000 rows , it's continuously updating.

now have search form need search data db table contact_details

for e.g : searching 2 in type column contact_details table , there 11,000 records of 2.

in situation, sql query taking long time produce result ! sometime it's showing me maximum time exceed. should result more ?

here contact_details table :

enter image description here enter image description here

here search form error message : enter image description here

i using following sql query search result :

if(!empty($ad_keyword)) {     $getsearch = "select * (select group_concat(distinct keywordname order keywordname) keywordname, "; } else{     $getsearch = "select "; } $getsearch .= " cd.cdid, cd.family_name, cd.given_name, cd.department, cd.title, company.*, users.nickname, contact_label.label_data              contact_details 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             left join contact_label on contact_label.cdid = cd.cdid             1=1 ";      if(!empty($ad_company)){         $getsearch .= "and company.company_name '$ad_company%' ";     }     if(!empty($ad_fname)){         $getsearch .= "and cd.family_name '$ad_fname%' ";     }     if(!empty($ad_department)){         $getsearch .= "and cd.department '$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' , '$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' , '$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 contact_label.label_data '%$ad_twork%'";         }     if(!empty($ad_tmobile)){             $getsearch .= "and cd.mobile_phone '%$ad_tmobile%'";        }      if(!empty($ad_resume)){             $getsearch .= "and locate('$ad_resume', contact_docs.file_content)";    //is 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') , '$ad_dateto' ";      }      if(!empty($ad_datefrom) && !empty($ad_dateto)){         $getsearch .= "and cd.created_date between '$ad_datefrom' , '$ad_dateto'";         }      if(!empty($ad_type)){             $getsearch .= "and cd.type = '$ad_type' ";       }      if(!empty($ad_wemail)){         $getsearch .= "and cd.email '$ad_wemail%'";         }      if(!empty($ad_pemail)){         $getsearch .= "and cd.email_private '$ad_pemail%'";         }      if(!empty($ad_title)){         $getsearch .= "and cd.title '$ad_title%'";      }      if(!empty($ad_source)){         $getsearch .= "and cd.source '$ad_source%'";        }       if(!empty($ad_consultant)){         $getsearch .= "and users.nickname '%$ad_consultant%'";      }      if(!empty($ad_keyword)){         $ad_keyword_param = str_replace(",","','",$ad_keyword);         $getsearch .= " , keywords.keywordname in ('$ad_keyword_param') ";     }      $getsearch .= " group cd.user_id, cd.cid, cd.cdid ";      if(!empty($ad_keyword)){         $ad_keyword_param = str_replace(",",",",$ad_keyword);         $getsearch .= " ) keywordname '$ad_keyword_param%' ";     } 

  1. implement indexing
  2. instead of fetch '*' specify required column name.
  3. instead of subquery try use join
  4. use 'limit' clause

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -