为什么 select 查询行为在 where 条件下使用单引号和不使用单引号时不同
why the select query behaviour is different with single quotes in where condition and without single quotes
我在执行带单引号和不带单引号的查询时观察到不同的行为。
使用单引号 ('121'
) 我很快就得到了结果并且查询很好地使用了索引。但是没有单引号,查询没有使用索引并且响应时间很长。
mysql> explain select * from tbl_n1 where id =121;
+----+-------------+----------------------+------+---------------+------+---------+------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------------+------+---------------+------+---------+------+--------+-------------+
| 1 | SIMPLE | tbl_n1 | ALL | n1_id | NULL | NULL | NULL | 286929 | Using where |
+----+-------------+----------------------+------+---------------+------+---------+------+--------+-------------+
1 row in set (0.00 sec)
mysql> explain select * from tbl_n1 where id ='121';
+----+-------------+----------------------+------+---------------+----------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------------+------+---------------+----------+---------+-------+------+-------------+
| 1 | SIMPLE | tbl_n1 | ref | n1_id | n1_id | 53 | const | 29 | Using where |
+----+-------------+----------------------+------+---------------+----------+---------+-------+------+-------------+
1 row in set (0.00 sec)
show create table tbl_n1\G
....
....
`id` varchar(10) DEFAULT NULL,
....
....
....
由于字段 id
的定义是 varchar
,如果没有单引号,数据库将不得不对字段 [=] 进行 隐式转换 10=] 应用强制转换(或任何将其转换为 varchar 的函数),这将阻止使用索引 idx_1055
阅读这篇关于它的文章:Type Conversion in Expression Evaluation
我在执行带单引号和不带单引号的查询时观察到不同的行为。
使用单引号 ('121'
) 我很快就得到了结果并且查询很好地使用了索引。但是没有单引号,查询没有使用索引并且响应时间很长。
mysql> explain select * from tbl_n1 where id =121;
+----+-------------+----------------------+------+---------------+------+---------+------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------------+------+---------------+------+---------+------+--------+-------------+
| 1 | SIMPLE | tbl_n1 | ALL | n1_id | NULL | NULL | NULL | 286929 | Using where |
+----+-------------+----------------------+------+---------------+------+---------+------+--------+-------------+
1 row in set (0.00 sec)
mysql> explain select * from tbl_n1 where id ='121';
+----+-------------+----------------------+------+---------------+----------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------------+------+---------------+----------+---------+-------+------+-------------+
| 1 | SIMPLE | tbl_n1 | ref | n1_id | n1_id | 53 | const | 29 | Using where |
+----+-------------+----------------------+------+---------------+----------+---------+-------+------+-------------+
1 row in set (0.00 sec)
show create table tbl_n1\G
....
....
`id` varchar(10) DEFAULT NULL,
....
....
....
由于字段 id
的定义是 varchar
,如果没有单引号,数据库将不得不对字段 [=] 进行 隐式转换 10=] 应用强制转换(或任何将其转换为 varchar 的函数),这将阻止使用索引 idx_1055
阅读这篇关于它的文章:Type Conversion in Expression Evaluation