Mysql columnname与';%%之间有区别吗';完全没有条件吗?
这两个查询(优化方面)有区别吗 及 我使用传递的参数在PHP中动态构建查询。 比如说Mysql columnname与';%%之间有区别吗';完全没有条件吗?,mysql,query-optimization,database-indexes,Mysql,Query Optimization,Database Indexes,这两个查询(优化方面)有区别吗 及 我使用传递的参数在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"; } $quer
$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时只使用一个索引。所以,如果我对firstname和lastname有单独的索引,那么只会使用一个索引。如果我的查询为:
select * from users where first_name like '%%' and last_name like '%%'
默认情况下,我可以在名字和姓氏上添加一个串联索引,搜索速度会快得多。大多数SQL服务器(我认为MySql就是其中之一)都会尽最大努力将索引与LIKE关键字结合使用
像“%”一样使用应该与大多数查询的无条件使用一样快。我不确定像“%”这样的
但在性能优化方面,通常需要记住两件重要的事情:
除非有问题,否则别担心
如果需要优化,则对其进行测量(跟踪工具、探查器等)
Like“%”与Like“%”或Like“%%”或Like“%%”相同
要自己检查这一点,只需对查询运行explain。请参阅我在表上运行的一些示例查询
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
与NULL
的比较总是产生NULL
,因此,如果表中的行的姓氏或姓氏为NULL,则其中LIKE“%”
(或“%”或“%”将不返回此行(因为NULL LIKE“%”
返回NULL
,这显然不是TRUE
).使用解释并查看差异。如果有什么不同。索引永远不会在“%”之类的上使用,以通配符开头会使索引毫无用处。确实有点离题,但很有趣!
select * from users where first_name like '%%' and last_name 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)