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的隐式转化这边并不是很清楚,所以就顺势整理了一下.希望对大家有所帮 ...
随机推荐
- 如何在CRM WebClient UI里使用HANA Live Report
1. 使用业务角色ANALYTICSPRO登录SAP CRM WebClient UI: 点击新建按钮创建一个新的HANA live report: 类型选择SHL: 弹出窗口,维护report的名称 ...
- cesium 动态水面效果
后续继续更新
- jQuery获取Select选择的Text和Value[转载]
语法解释:1. $("#select_id").change(function(){//code...}); //为Select添加事件,当选择其中一项时触发2. var ch ...
- eplise一键集成工具
因为要做平台,后台的内容就由我负责,目前想让测试人员 在本地使用eplise可以进行脚本开发,但是很多人都死在了搭建环境的道路上,那我就做了一键集成,点击就可以把所需要的配置项进行配置,总结:实际就 ...
- 火车进站输出路径(HDU1022)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1022 解题报告: 思路: 就是维护好这个栈,只要它不是空,并且头部和ans相同,就一直出栈,直到不满足 ...
- ssh key一键自动化生成公钥私钥,并自动分发上百服务器免密码交互
题记:由于工作需要管理大量服务器,所以需要配公钥实现免密登录. ssh批量分发可以一键执行这个操作,但是使用ssh分发服务还需要对各个服务器进行.ssh/id_dsa.pub公钥上传,密码验证.所以需 ...
- 调用外部EXE文件
实现效果: 知识运用: Process类的Start方法 实现代码: private void button1_Click(object sender, EventArgs e) { OpenFile ...
- 2018.7.3 JS实现增删改查没有连接数据库
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- 2017.11.10 web中URL和URI的区别
URI:Uniform Resource Identifier,统一资源标识符: •URL:Uniform Resource Locator,统一资源定位符: •URN:Uniform Resourc ...
- 第14章 启动文件详解—零死角玩转STM32-F429系列
第14章 启动文件详解 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/firege ...