像“%%”这样的列名之间有区别吗?而且根本没有条件?
这两个查询有区别吗(优化方面)?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Like '%' 与 Like '%%' 或 Like '%%%' 或 LIKE '%%%%' 相同。
要亲自检查这一点,只需对查询运行解释即可。这是我在表上运行的一些示例查询。
@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.
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.
大多数 SQL 服务器(我认为 MySql 就是其中之一)都尽力将索引与 LIKE 关键字结合使用。
对于大多数查询来说,使用
LIKE '%'
应该与无条件一样快。我不确定LIKE '%%'
但一般来说,在性能优化方面需要记住两件重要的事情:
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 aboutLIKE '%%'
But generally there are two important things to remember when it comes to performance optimisation:
//编辑:很晚才读到你问题的第一行,所以我错过了“优化明智”部分......现在我的答案有点偏离主题,但并非完全错误,所以我不会删除它。也许有人觉得它有用......
关于索引的很多事情已经说过了,所以我没有什么可补充的。
但还有一个重要的点可能会或可能不会妨碍您,具体取决于您的表设置:
LIKE
与NULL
比较始终会产生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 toNULL
always yieldsNULL
, so if your table has rows in which last_name or first_name is NULL, thenWHERE <field> LIKE '%'
(or '%%' or '%%%') will not return this row (becauseNULL LIKE '%'
returnsNULL
which is obviously notTRUE
).