MySQL隐式转换测试
(zlm@192.168.1.101 )[zlm]>select cast('2abc' as unsigned) from dual;
+--------------------------+
| cast('2abc' as unsigned) |
+--------------------------+
| |
+--------------------------+
row in set, warning (0.00 sec) (zlm@192.168.1.101 )[zlm]>select convert('2abc',unsigned) from dual;
+--------------------------+
| convert('2abc',unsigned) |
+--------------------------+
| |
+--------------------------+
row in set, warning (0.00 sec) //Function cast() and convert() turned the string '2abc' to number.
//The result became 2 what means the string begin with number only keep the first number remain. (zlm@192.168.1.101 )[zlm]>select convert('abc',unsigned) from dual;
+-------------------------+
| convert('abc',unsigned) |
+-------------------------+
| |
+-------------------------+
row in set, warning (0.00 sec) (zlm@192.168.1.101 )[zlm]>select cast('abc' as unsigned) from dual;
+-------------------------+
| cast('abc' as unsigned) |
+-------------------------+
| |
+-------------------------+
row in set, warning (0.00 sec) //If there's no number prefix,the result turns out to be zero(integer type here). (zlm@192.168.1.101 )[zlm]>select ''=convert('3abc',unsigned);
+-----------------------------+
| ''=convert('abc',unsigned) |
+-----------------------------+
| |
+-----------------------------+
row in set, warning (0.00 sec) (zlm@192.168.1.101 )[zlm]>select ''=cast('3abc' as unsigned);
+-----------------------------+
| ''=cast('abc' as unsigned) |
+-----------------------------+
| |
+-----------------------------+
row in set, warning (0.00 sec) (zlm@192.168.1.101 )[zlm]>select =convert('3abc',unsigned);
+-----------------------------+
| ''=convert('abc',unsigned) |
+-----------------------------+
| |
+-----------------------------+
row in set, warning (0.00 sec) (zlm@192.168.1.101 )[zlm]>select =cast('3abc' as unsigned);
+-----------------------------+
| ''=cast('abc' as unsigned) |
+-----------------------------+
| |
+-----------------------------+
row in set, warning (0.00 sec) //All the rusults are 1 what means the implicit conversion occurs.
Test the implicit conversion in expression and function concat().
(zlm@192.168.1.101 )[zlm]>select +'abc' from dual;
+----------+
| +'abc' |
+----------+
| |
+----------+
row in set, warning (0.01 sec) //The string 'abc' converted implicitly from string to number and got result of 10(10 + 0 = 0). (zlm@192.168.1.101 )[zlm]>select concat(,'abc') from dual;
+------------------+
| concat(,'abc') |
+------------------+
| 10abc |
+------------------+
row in set (0.00 sec) //The number 10 was converted implicitly to string and concantenated by string 'abc'. (zlm@192.168.1.101 )[zlm]>select +'abc'=concat(,'abc');
+---------------------------+
| +'abc'=concat(,'abc') |
+---------------------------+
| |
+---------------------------+
row in set, warning (0.00 sec) //What may astonish us is that the result became 1,that is,implicit conversion occured again. (zlm@192.168.1.101 )[zlm]>select =concat(,'abc')-'abc';
+---------------------------+
| =concat(,'abc')-'abc' |
+---------------------------+
| |
+---------------------------+
row in set, warning (0.00 sec) //Moving the string 'abc' to the right side didn't influence the result of 1.
Test the implicit conversion in table.
(zlm@192.168.1.101 )[sysbench]>desc sbtest1;
+-------+-----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------+------+-----+---------+----------------+
| id | int() | NO | PRI | NULL | auto_increment |
| k | int() | NO | MUL | | |
| c | char() | NO | | | |
| pad | char() | NO | | | |
+-------+-----------+------+-----+---------+----------------+
rows in set (0.00 sec) (zlm@192.168.1.101 )[sysbench]>select * from sbtest1 limit ;
+----+------+-------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------+
| id | k | c | pad |
+----+------+-------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------+
| | | --------- | ---- |
| | | --------- | ---- |
| | | --------- | ---- |
| | | --------- | ---- |
| | | --------- | ---- |
+----+------+-------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------+
rows in set (0.00 sec) (zlm@192.168.1.101 )[sysbench]>explain select * from sbtest1 where id='';
+----+-------------+---------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| | SIMPLE | sbtest1 | NULL | const | PRIMARY | PRIMARY | | const | | 100.00 | NULL |
+----+-------------+---------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
row in set, warning (0.00 sec) (zlm@192.168.1.101 )[sysbench]>explain select * from sbtest1 where id=cast( as char);
+----+-------------+---------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| | SIMPLE | sbtest1 | NULL | const | PRIMARY | PRIMARY | | const | | 100.00 | NULL |
+----+-------------+---------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
row in set, warning (0.00 sec) //Despite I've specified 1 as string above,the queries still return result with primary key. (zlm@192.168.1.101 )[sysbench]>alter table sbtest1 add unique(c); //Add a unique key on column c(char type).
Query OK, rows affected ( min 58.75 sec) //It cost almost 5 mins.
Records: Duplicates: Warnings: (zlm@192.168.1.101 )[sysbench]>desc sbtest1;
+-------+-----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------+------+-----+---------+----------------+
| id | int() | NO | PRI | NULL | auto_increment |
| k | int() | NO | MUL | | |
| c | char() | NO | UNI | | |
| pad | char() | NO | | | |
+-------+-----------+------+-----+---------+----------------+
rows in set (0.00 sec) (zlm@192.168.1.101 )[sysbench]>select count(*) from sbtest1;
+----------+
| count(*) |
+----------+
| |
+----------+
row in set (3.52 sec) //Query 1
(zlm@192.168.1.101 )[sysbench]>explain select * from sbtest1 where c=---------;
+----+-------------+---------+------------+------+---------------+------+---------+------+---------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+------+---------------+------+---------+------+---------+----------+-------------+
| | SIMPLE | sbtest1 | NULL | ALL | c | NULL | NULL | NULL | | 10.00 | Using where |
+----+-------------+---------+------------+------+---------------+------+---------+------+---------+----------+-------------+
row in set, warnings (0.00 sec) //Query 2
(zlm@192.168.1.101 )[sysbench]>explain select * from sbtest1 where c='83868641912-28773972837-60736120486-75162659906-27563526494-20381887404-41576422241-93426793964-56405065102-33518432330';
+----+-------------+---------+------------+-------+---------------+------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+-------+---------------+------+---------+-------+------+----------+-------+
| | SIMPLE | sbtest1 | NULL | const | c | c | | const | | 100.00 | NULL |
+----+-------------+---------+------------+-------+---------------+------+---------+-------+------+----------+-------+
row in set, warning (0.01 sec) //Query 3
(zlm@192.168.1.101 )[sysbench]>explain select * from sbtest1 where c like ;
+----+-------------+---------+------------+------+---------------+------+---------+------+---------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+------+---------------+------+---------+------+---------+----------+-------------+
| | SIMPLE | sbtest1 | NULL | ALL | c | NULL | NULL | NULL | | 11.11 | Using where |
+----+-------------+---------+------------+------+---------------+------+---------+------+---------+----------+-------------+
row in set, warnings (0.00 sec) //Query 4
(zlm@192.168.1.101 )[sysbench]>explain select * from sbtest1 where c like '83868641912%';
+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-----------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-----------------------+
| | SIMPLE | sbtest1 | NULL | range | c | c | | NULL | | 100.00 | Using index condition |
+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-----------------------+
row in set, warning (0.00 sec) //The implicit conversion occured in query 1 and query 3.They cannot use the index on column c.
//In query 4,it even used ICP feature of MySQL(Which is supported since version 5.6).
- The implicit conversion usually occurs to make the query to be as compatible as possible in MySQL.
- The implicit conversion occurs in expression,function and condiction of queries.
- The implicit conversion may lead to bad performance because it will prevent MySQL from using indexes on specific query columns.
- We'd better specify the correct data type explicit when typing them in our query SQL statement to avoid the implicit conversion.
MySQL隐式转换测试的更多相关文章
- MySQL隐式转换的坑
MySQL以以下规则描述比较操作如何进行转换: 两个参数至少有一个是 NULL 时,比较的结果也是 NULL,例外是使用 <=> 对两个 NULL 做比较时会返回 1,这两种情况都不需要做 ...
- 一个 MySQL 隐式转换的坑,差点把服务器整崩溃了
我是风筝,公众号「古时的风筝」,专注于 Java技术 及周边生态. 文章会收录在 JavaNewBee 中,更有 Java 后端知识图谱,从小白到大牛要走的路都在里面. 本来是一个平静而美好的下午,其 ...
- 关于MySQL隐式转换
一.如果表定义的是varchar字段,传入的是数字,则会发生隐式转换. 1.表DDL 2.传int的sql 3.传字符串的sql 仔细看下表结构,rid的字段类型: 而用户传入的是int,这里会有一个 ...
- Mysql 隐式转换
表定义: CREATE TABLE `ids` ( id ) not null auto_increment, PRIMARY KEY (id) ); 表中存在一些IDs: 111, 112, 113 ...
- Mysql隐式类型转换原则
MySQL 的隐式类型转换原则: - 两个参数至少有一个是 NULL 时,比较的结果也是 NULL,例外是使用 <=> 对两个 NULL 做比较时会返回 1,这两种情况都不需要做类型转换 ...
- MySQL SQL优化之字符串索引隐式转换
之前有用户很不解:SQL语句非常简单,就是select * from test_1 where user_id=1 这种类型,而且user_id上已经建立索引了,怎么还是查询很慢? test_1的表结 ...
- MySQL性能优化:MySQL中的隐式转换造成的索引失效
数据库优化是一个任重而道远的任务,想要做优化必须深入理解数据库的各种特性.在开发过程中我们经常会遇到一些原因很简单但造成的后果却很严重的疑难杂症,这类问题往往还不容易定位,排查费时费力最后发现是一个很 ...
- mysql中的隐式转换
在mysql查询中,当查询条件左右两侧类型不匹配的时候会发生隐式转换,可能导致查询无法使用索引.下面分析两种隐式转换的情况 看表结构 phone为 int类型,name为 varchar EXPLAI ...
- MySQL隐式转化整理
MySQL隐式转化整理 前几天在微博上看到一篇文章:价值百万的 MySQL 的隐式类型转换感觉写的很不错,再加上自己之前也对MySQL的隐式转化这边并不是很清楚,所以就顺势整理了一下.希望对大家有所帮 ...
随机推荐
- 修改hosts工具推荐SwitchHosts
推荐一个修改hosts的工具.适合平时工作中经常修改hosts的开发测试. 下载地址:https://oldj.github.io/SwitchHosts/ 可以按各种环境或者项目添加,用的时候打开或 ...
- SQL语句关于时间的查询小心得,希望大家给点意见
完全使用时间函数去搞定查询日期,之前写的可能有些问题,现在删了修正一下 本月记录: SELECT * FROM 表 WHERE datediff(month,[dateadd],getdate())= ...
- ios 逆向工程文档汇总
iOS逆向工程工具集 http://www.jianshu.com/p/7f9511d48e05 移动App入侵与逆向破解技术-iOS篇 http://blog.csdn.net/heiby/arti ...
- IBM带库加磁带操作
1.查询要弹出磁带的信息 可查询media日志,冻结,可用等,详情可查 查看带库空闲槽位 vmcheckxxx -rt tld -rn 0(0为带库名) 磁带详细信息: bpmedialist -m ...
- 解决Gearman 报sqlite3错误
删除了系统原带的sqlite3 ,到官网上下一个源码,重新编译安装sqlite3. 如:把sqlite3安装到 /usr/local/sqlite3tar zxf sqlite3.xxxx.tar.g ...
- python 面向对象(二)--访问限制
在Class内部,可以有属性和方法,而外部代码可以通过直接调用实例变量的方法来操作数据,这样,就隐藏了内部的复杂逻辑. 但是,从前面Student类的定义来看,外部代码还是可以自由地修改一个实例的na ...
- 整个trick
数据输入方面:1.image pyramid 图像金字塔.目前代码里是先选取一个scale,然后在每个GPU上按照scale读图片,相应的gt也更改."scales":[440, ...
- 如何安装Ruby(Windows)
Ruby解释器的安装 1.Windows平台 想尽快安装并运行Ruby,可遵循如下步骤: 1.启动Web浏览器,访问 http://www.ruby-lang.org/en/downloads/ 2. ...
- data-ng-app 指令
1.data-ng-app指令定义了一个AngularJS应用程序的根元素. 2.data-ng-app会在页面加载完毕后自动进行初始化应用程序. 3.data-ng-app可以通过一个值连接到代码模 ...
- 批量删除xml文件中的<?xml version="1.0" ?>
#!/bin/shcd 'home/usrname/'ls cd '/home/usrname/VOC2007/Annotations/' for file in `ls /home/usrname/ ...