2011-02-24 60 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