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的隐式转化这边并不是很清楚,所以就顺势整理了一下.希望对大家有所帮 ...
随机推荐
- 其他信息: 尝试加载 Oracle 客户端库时引发 BadImageFormatException。如果在安装 32 位 Oracle 客户端组件的情况下以 64 位模式运行,将出现此问题。在VS中的解决方法
第一种,不使用dataaccess,使用通用的Oracle.ManagedDataAccess,可以忽略版本问题 第二种,在属性——生成——web中iis express换成用本地IIS执行,但是这样 ...
- 20145238-荆玉茗 《Java程序设计》第一周学习总结
20145238 <Java程序设计>第一周学习总结 教材学习内容总结 Java三大平台:由于java领域的应用越来越广,根据不同级别的应用开发区分了不同的应用版本,后正式更名为Java ...
- arXiv 上传文章过程
arXiv属于预印本服务的一种,是指科研工作者的研究成果还未在正式出版物上发表,而出于和同行交流目的自愿先在学术会议上或通过互联网发布的科研论文.科技报告等文章.与刊物发表的文章以及网页发布的文章 ...
- GPU计算的后CUDA时代-OpenACC(转)
在西雅图超级计算大会(SC11)上发布了新的基于指令的加速器并行编程标准,既OpenACC.这个开发标准的目的是让更多的编程人员可以用到GPU计算,同时计算结果可以跨加速器使用,甚至能用在多核CPU上 ...
- Git永久删除commit--[非教程]
假设当前分支为master,当前的commit情况如下,现在需要删除commit_id_2和commit_id_4: commit_id_1 commit_id_2 commit_id_3 commi ...
- js 动态创建标记
innerHTML:一旦使用了这个属性,它的全部内容都要被替换掉.且不会返回任何对刚插入的内容的引用 与document.write()方法一样,innerHTML属性也是HTML专有属性,不能用于任 ...
- Junit 测试 @Test 红名问题
准备测试,如上图,都是红名 Ctrl+1 或者鼠标放在@Test上 鼠标放在@Test上出现上面这种就可直接点击安装了 如果是下边这种 Test is not an annotation type,一 ...
- java多线程同步以及线程间通信详解&消费者生产者模式&死锁&Thread.join()(多线程编程之二)
本篇我们将讨论以下知识点: 1.线程同步问题的产生 什么是线程同步问题,我们先来看一段卖票系统的代码,然后再分析这个问题: package com.zejian.test; /** * @author ...
- Tomcat的部署+第一个Servlet
Tomcat部署 1.下载tomcat,添加到eclipse 2.配置环境变量(path) 3.win+r,输入Startup(如果没用,就管理员启动命令) 或者找到tomcat安装包,在bin目录找 ...
- 爬虫学习(十五)——json解析
json与jsonpath 对象{}:jsonobject 对象:对象在js中表现为{}括起来的内容,数据结构为{key:value,key:value...}键值对的结构,在面向对象的结构中,key ...