我在使用单引号执行查询时没有观察到单行引号的不同行为。为什么选择查询行为与单引号不同,在哪里条件且没有单引号
用单引号('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,
....
....
....
没问题,我会删除评论和感谢! –