像“%%”这样的列名之间有区别吗?而且根本没有条件?

发布于 2024-10-19 03:37:45 字数 773 浏览 10 评论 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 '%%'

默认情况下,我可以在名字和姓氏上添加串联索引,并且搜索会更快?

Is there a difference in these 2 queries (optimization wise) ?

select * from users;

and

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

I am building the query in PHP dynamically using the parameters passed.
So, for example..

$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";

The reason i am asking this is because i read that mysql uses only one index while doing a select. So, if i have individual indexes on firstname and lastname, only one will be used. In the case that i have the query as :

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

by default, i can add a concatenated index on both first_name and last_name and searching will be much faster ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

幽梦紫曦~ 2024-10-26 03:37:45

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

Like '%' is same as Like '%%' or Like '%%%' or LIKE '%%%%'.

To check this yourself just run explain on the query. Se some example queries I ran on my table.

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)

2 points of @Iain are right way to go about performance.
But try to locate most of the performance problems in staging using load testing.

梦里泪两行 2024-10-26 03:37:45

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

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

但一般来说,在性能优化方面需要记住两件重要的事情:

  1. 除非它是一个问题,否则不要担心
  2. 如果需要优化,请对其进行衡量(跟踪工具、分析器等)

Most SQL servers (and I think MySql is one of them) do their best to use indexes well with the LIKE keyword.

Using LIKE '%' should be as fast as no condition for most queries. I'm not sure about LIKE '%%'

But generally there are two important things to remember when it comes to performance optimisation:

  1. Don't worry unless it is a problem
  2. If it needs optimising, measure it (tracing tools, profilers etc.)
云裳 2024-10-26 03:37:45

//编辑:很晚才读到你问题的第一行,所以我错过了“优化明智”部分......现在我的答案有点偏离主题,但并非完全错误,所以我不会删除它。也许有人觉得它有用......

关于索引的很多事情已经说过了,所以我没有什么可补充的。

但还有一个重要的点可能会或可能不会妨碍您,具体取决于您的表设置:

LIKENULL 比较始终会产生 NULL,因此,如果您的表中的行中姓氏或名字为 NULL,则 WHERE; LIKE '%' (或 '%%' 或 '%%%')不会返回此行(因为 NULL LIKE '%' 返回 NULL显然不是TRUE)。

//EDIT: Did read the first line of your question to late, so I missed the "optimization wise" part... Now my answer is a bit off topic, but not entirely wrong, so I'm not going to delete it. Maybe someone finds it useful anyway...

Many things about indexes have already been said, so I have nothing to add.

But there's another important point that may or may not come in your way, depends on your table setup:

LIKE comparison to NULL always yields NULL, so if your table has rows in which last_name or first_name is NULL, then WHERE <field> LIKE '%' (or '%%' or '%%%') will not return this row (because NULL LIKE '%' returns NULL which is obviously not TRUE).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文