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的隐式转化这边并不是很清楚,所以就顺势整理了一下.希望对大家有所帮 ...
随机推荐
- Xapian简明教程(未完成)
第一章 简介 1.1 简介 Xapian是一个开源的搜索引擎库,它可以让开发者自定义的开发一些高级的的索引和查找因素应用在他们的应用中. 通过阅读这篇文档,希望可以帮助你创建第一个你的索引数据库和了解 ...
- 将命令的输出生成一个Web页面
解决方法: ConvertTo-Html 命令: 生成一个HTML表格来代表命令的输出,为你提供的每个对象创建一行,在每行中,Powershell会创建代表对象属性的值. 实现效果:
- OnCollisionEnter和OnTriggerEnter
之前对这两个的用法不是特别清楚,重新学习了下,再做个测试看看效果如何: 1.新建一个场景test 2.添加一个cube,点击Inspector面板会发现系统已经默认添加了Box collisder组件 ...
- mysql随机字符串函数
drop function if exists rand_str; delimiter $$ ) charset 'utf8' begin # 定义接收初始化类型 ) ; # 定义初始化数字 ) '; ...
- phpmyadmin高级功能尚未完全设置部分功能未激活
1.登录phpmyadmin,点击导入,选择/var/ww/html/phpmyadmin/examples/create_tables.sql并执行 完成后可以看到多出了一个库phpmyadmin. ...
- innerHTML动态添加标签的注意事项
在使用javascript动态添加页面上元素时,我们经常会使用DOM去逐个地将节点添加到文档碎片中,再将整个文档节点添加到DOM树中.其实还有一种方法动态添加元素:innerHTML. 我最近要将一大 ...
- javascript入门笔记4-数组
1.数组 var arr=new Array(); var myarray= new Array(8); //创建数组,存储8个数据. 注意: 1.创建的新数组是空数组,没有值,如输出,则显示unde ...
- Windows/Linux下查看系统CPU使用最高的线程
参考:https://blog.csdn.net/qq_27818157/article/details/78688580 jstack -l 31372 > c:/31372.stack
- 第34-3题:LeetCode437. Path Sum III
题目 二叉树不超过1000个节点,且节点数值范围是 [-1000000,1000000] 的整数. 示例: root = [10,5,-3,3,2,null,11,3,-2,null,1], sum ...
- 没有CTO的Netflix有哪些值得我们学习的工程文化?
作者介绍: 杨波,拍拍贷基础框架研发总监.具有超过 10 年的互联网分布式系统研发和架构经验,曾先后就职于:eBay 中国研发中心(eBay CDC),任资深研发工程师,参与亿贝开放 API 平台研发 ...