我正在使用Mysql 5.5.16。我有一個查詢,我使用union opeartor結合了3個查詢...每個查詢包含超過10000條記錄。如何減少Mysql執行時間?
我的查詢看起來像......
$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
由於事先
傑尼
是否以任何方式通過索引搜索任何列? –
請發佈最終的SQL查詢,因爲它只是關於它,而不是PHP。 –
也可以將解釋的輸出添加到不同的SQL語句中。 – MatsLindh