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 上添加连接索引,搜索会更快吗?

4

3 回答 3

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 点是提高性能的正确方法。但是尝试使用负载测试来定位登台中的大部分性能问题。

于 2011-02-24T17:08:09.153 回答
0

大多数 SQL 服务器(我认为 MySql 就是其中之一)尽最大努力将索引与 LIKE 关键字一起使用。

对于大多数查询,使用LIKE '%'应该与无条件一样快。我不确定LIKE '%%'

但在性能优化方面,通常有两件重要的事情需要记住:

  1. 除非有问题,否则不要担心
  2. 如果需要优化,请测量它(跟踪工具、分析器等)
于 2011-02-24T08:48:23.353 回答
0

//编辑:是否阅读了您问题的第一行,所以我错过了“优化明智”部分......现在我的回答有点偏离主题,但并非完全错误,所以我不会删除它. 也许有人发现它还是有用的......

关于索引的很多东西已经说了,所以我没有什么要补充的。

但是还有一个重要的点可能会或可能不会出现在您的方式中,这取决于您的桌子设置:

LIKENULLalways yield比较NULL,因此如果您的表中有 last_name 或 first_name 为 NULL 的行,那么WHERE <field> LIKE '%'(或 '%%' 或 '%%%')将不会返回该行(因为NULL LIKE '%'返回NULL显然不是TRUE)。

于 2011-02-24T17:19:47.310 回答