我正在使用Mysql 5.5.16。我有一个查询,我使用union opeartor组合3个查询...每个查询包含超过10000条记录。
我的查询看起来像......
$search_qry1 = " (SELECT PC_name as name FROM profile_category_tbl WHERE PC_status=1 and PC_parentid!=0 and (";
$search_qry2 = " (SELECT PROFKEY_name as name FROM profile_keywords_tbl WHERE PROFKEY_status=1 and (";
$search_qry3 = " (SELECT COM_name as name FROM ".$C."_company_profile_tbl WHERE COM_status=1 and (";
$order_by = "ORDER BY";
$word_length = explode(" ", $my_data);
for($i=0; $i <= count($word_length) - 1; $i++) {
$dt = $word_length[$i];
if ($dt) {
if ($i == 0) {
$or="";
}
else {
$or="OR";
}
$search_qry1 .= " $or regex_replace('[^a-zA-Z0-9\-]','',remove_specialCharacter(PC_name)) LIKE '%$dt%' ";
$search_qry2 .= " $or regex_replace('[^a-zA-Z0-9\-]','',remove_specialCharacter(PROFKEY_name)) LIKE '%$dt%' ";
$search_qry3 .= " $or regex_replace('[^a-zA-Z0-9\-]','',remove_specialCharacter(COM_name)) LIKE '%$dt%' ";
$order_by .= " IF(name LIKE '%$dt%',1,0) ";
}
if ($i == count($word_length) - 1) {
$search_qry1 .= ")";
$search_qry2 .= ")";
$search_qry3 .= ")";
}
if ($i != count($word_length) - 1) {
$order_by .= "+ ";
}
}
$search_qry1 .= " GROUP BY PC_name )";
$search_qry2 .= " GROUP BY PROFKEY_name )";
$search_qry3 .= " GROUP BY COM_name )";
$search_qry = "select name from ( $search_qry1 UNION ALL $search_qry2 UNION ALL $search_qry3 )a $order_by desc LIMIT 0,25";
它工作得很完美......但它需要时间来执行它 - 超过4秒...每次搜索......如何才能减少执行时间?....如果有人知道一个想法请告诉我....
实际上我运行此查询以进行自动完成搜索。如果在搜索框中输入“Rest
”,则
The Output Should be,
Family Restaurants
Everest Park
Fast Food Restaurants
Everest Park Residency
Fish Restaurant
Everest Power Solution
Fish Restaurants
先谢谢,
杰尼
答案 0 :(得分:0)
您正在使用regex_replace
和like "%foobar%"
,
这实际上会减慢你的查询意义,因为mySql必须在每一行上进行全文搜索和正则表达式操作。也许如果你在使用php而不是mysql获取结果后执行这些操作会更快。
答案 1 :(得分:0)
主要问题是你正在对每个表进行全面扫描,以检查类似'%$ dt%'的情况。无论你在桌子上有哪些索引,都不会使用它们。
但是如果你发布最后的SQL句子,你想做什么,也许我们可以提供帮助。