2011-02-24 59 views
0

這兩個查詢有沒有區別(優化明智)?像「%%」這樣的列名和沒有條件有區別嗎?

select * from users; 

select * from users where first_name like '%%' and last_name like '%%' 

我建立查詢在PHP中使用動態傳遞的參數。 因此,例如..

$first_name_str = ""; 
if($firstname) 
{ 
    $first_name_str = "first_name = '%".$firstname."%' and"; 
} 

$last_name_str = ""; 
if($lastname) 
{ 
    $last_name_str = "last_name = '%".$lastname."%' and"; 
} 


$query = 
"select 
     * 

from  
    users 

where 
    ".$first_name_str." 
    ".$last_name_str." 
    1=1"; 

的原因,我問這個是因爲我讀的MySQL只使用一個索引,而做一個選擇。所以,如果我有姓和名的個人索引,只會使用一個。住在我作爲查詢的情況:

select * from users where first_name like '%%' and last_name like '%%' 

默認情況下,我可以同時FIRST_NAME添加級聯指數和last_name和搜索會快很多?

+0

使用EXPLAIN並查看其差異。如果有任何區別。一個索引永遠不會在'LIKE'%%''上使用,從通配符開始使索引無用。 – 2011-02-24 09:33:48

回答

2

Like'%'與Like'%%'或Like'%%%'或LIKE'%%%%'相同。

要自己檢查,只需在查詢上運行解釋。查看我在桌面上運行的一些示例查詢。

mysql> explain select * from USERS where EMAIL like '%'; 
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+ 
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra  | 
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+ 
| 1 | SIMPLE  | USERS | ALL | NULL   | NULL | NULL | NULL | 415 | Using where | 
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+ 
1 row in set (0.02 sec) 

mysql> explain select * from USERS where EMAIL like '%%'; 
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+ 
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra  | 
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+ 
| 1 | SIMPLE  | USERS | ALL | NULL   | NULL | NULL | NULL | 415 | Using where | 
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+ 
1 row in set (0.00 sec) 

mysql> explain select * from USERS where EMAIL like '%%%'; 
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+ 
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra  | 
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+ 
| 1 | SIMPLE  | USERS | ALL | NULL   | NULL | NULL | NULL | 415 | Using where | 
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+ 
1 row in set (0.01 sec) 

@Iain的2分是正確的表現方式。 但嘗試查找使用負載測試進行暫存時的大部分性能問題。

0

大多數SQL服務器(我認爲MySql就是其中之一)盡最大努力使用LIKE關鍵字的索引。

對於大多數查詢,使用LIKE '%'應該和沒有條件一樣快。我不知道LIKE '%%'

但一般有要記住,當涉及到性能優化兩個重要的事情:

  1. 不要擔心,除非這是一個問題
  2. 如果需要優化,測量它(跟蹤工具,分析器等)
0

//編輯:你的問題的第一行遲到了,所以我錯過了「優化智慧」的一部分......現在我的回答有點偏離主題,但並非完全錯誤,所以我不是要刪除它。也許有人發現它有用...

關於索引的許多事情已經說過了,所以我沒有什麼補充。

但有可能會或可能不會在你的方式來另一個重要的一點,取決於你的表設置:

LIKE比較NULL總會讓步NULL,所以如果你的表有行中,姓氏或FIRST_NAME爲NULL ,那麼WHERE <field> LIKE '%'(或'%%'或'%%%')將不會返回此行(因爲NULL LIKE '%'返回NULL,顯然不是TRUE)。

+0

確實有點偏離主題,但有趣! – Galz 2011-02-26 22:30:51