mysql where子句用法,mysql默认类型转化

发布时间:2019-07-29编辑:脚本学堂
有关mysql where子句实现默认类型转化的方法,mysql将id默认转化为int型,然后进行比较,索引中包含select出来的二个字段,mysql query优化器就选择使用id_source索引。

mysql where子句默认类型转化

1、创建数据表:
 

复制代码 代码示例:
CREATE TABLE `table1` (
`id` varchar(20) collate utf8_bin NOT NULL default '',
`from` varchar(128) collate utf8_bin NOT NULL default '',
`time` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `id_source` (`id`,`from`)
) ENGINE=innodb DEFAULT CHARSET=utf8 COLLATE=utf8_bin

语句:
 

复制代码 代码示例:
select id, from from table1 where id=325381768;

执行居然花费了40多秒钟,

为什么id用varchar,这里主要是为了从根本上说明这条sql为什么会慢。
 

复制代码 代码示例:
xxx.host/fb>explain select id, from from table1 where id=325381768;
+----+-------------+-----------------+-------+-------------------+-----------+---------+------+----------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------------+-------+-------------------+-----------+---------+------+----------+--------------------------+
| 1 | SIMPLE | reg_from | index | PRIMARY,id_source | id_source | 448 | NULL | **** | Using where; Using index |
+----+-------------+-----------------+-------+-------------------+-----------+---------+------+----------+--------------------------+
1 row in set (0.00 sec)

虽然explain的结果是使用到了索引,但是结果执行结果确时间很长。
 

复制代码 代码示例:

xxx.host/fb>select id, from from table1 where cast(id as signed)=325381768;
+-----------+-------------+
| id | from |
+-----------+-------------+
| 325381768 | S_SCREG; |
+-----------+-------------+
1 row in set (51.81 sec)

xxx.host/fb>select id, from from table1 where id=cast(325381768 as char);
+-----------+-------------+
| id | from |
+-----------+-------------+
| 325381768 | S_SCREG; |
+-----------+-------------+
1 row in set (0.00 sec)

从上面这个对比试验中可以看出,mysql是将id默认转化成了int型,然后才进行的比较,问题的症结就在这里,因为进行了转化操作所以就不能再使用索引了,那为什么explain的结果仍然是使用了索引,因为索引中包含了需要select出来的两个字段,因此mysql query优化器就选择了使用id_source索引。

通过观察Handler_read_first的状态值就可以确认mysql对id_source索引进行了一个full scan。
 

复制代码 代码示例:

xxx.host/fb>show status like "Handler_read_first";
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| Handler_read_first | 2 |
+--------------------+-------+
1 row in set (0.02 sec)

xxx.host/fb>select id, from from table1 where id=325381768;
+-----------+-------------+
| id | from |
+-----------+-------------+
| 325381768 | S_SCREG; |
+-----------+-------------+
1 row in set (43.02 sec)

xxx.host/fb>show status like "Handler_read_first";
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| Handler_read_first | 3 |
+--------------------+-------+
1 row in set (0.02 sec)

在文档中找到“Comparison operations result in a value of 1 (TRUE), 0 (FALSE), or NULL. These operations work for both numbers and strings. Strings are automatically converted to numbers and numbers to strings as necessary.”证实以上推测是正确的。

文档地址:http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_equal