新来的实习生把数据库搞炸了......

运维 数据库运维
SQL 写的秒,涨薪呱呱叫!就在前不久公司新来的实习生因为写错了一条SQL把数据库搞炸了。

 SQL 写的秒,涨薪呱呱叫!就在前不久公司新来的实习生因为写错了一条SQL把数据库搞炸了。

[[334989]]

 

图片来自 Pexels

新来的实习生小杨写了一条 SQL 语句:

  1. SELECT wx_id from `userWHERE wx_id = 2 

当小杨迫不及待准备下班回家的时候,隔壁的王经理一把抓住了小杨,并用 EXPLAIN 命令教育了小杨,小杨流下了没有文化的泪水。

这条 SQL 语句中,wx_id 是具有索引的,但是王经理查出来的结果却是这样的:

 

王经理的教育

小杨仔细一瞅 key 字段显示为 Null,很明显这条 SQL 语句没有走索引。

小杨心想“糟糕,又写错 SQL 语句了,这下又要面临运维和经理的混合双打了, 不行我得立马改下这条 SQL 语句,让我想想哪里出错了”!

[[334990]]

 

小杨脑袋瓜疯狂乱撞,仔细回想表结构,忽然想到,wx_id 字段是 varchar 类型,自己查询的时候竟然没有加引号。

小杨一把抢过经理手里的键盘,往 wx_id 的查询条件上加了引号,结果:

果然这条 SQL 语句开始走了索引。小杨沾沾自喜以为解决了个天大的 Bug。

 

经理微微一笑问道“你知道为什么为什么加了引号就走了索引吗?如果字段是 int 类型,那么查询的时候需不需要加引号呢?又是为什么呢?”

正餐来了

小杨被问的呆在原地,无法回答。

经过小杨研究发现,如果字段是 varchar类型,等号右侧必须加引号才走索引;如果字段是 int 类型,那么等号右侧加不加引号都是会走索引的。

什么?你不相信小杨说的话,有图有真相。(bonus 字段类型为int)

 

真相图

但是结论出来,还是无法回答经理的夺命三连问。

小杨搬来了答案

在 MySQL 查询中,当查询条件左右两侧类型不匹配的时候会发生隐式转换:

  1. 也就是说 
  2. SELECT wx_id from `userWHERE wx_id = 2 
  3. 等价于 
  4. SELECT wx_id from `userWHERE CAST(wx_id AS signed int) = 2 

一旦对索引字段做函数操作,MySQL 会放弃使用索引。

所以如果字段是 varchar 类型,等号右侧必须加引号才走索引,否则由于隐式转换,MySQL 会放弃使用索引。那么凭什么 int 加不加引号都可以使用索引呢?

那是因为 int 类型的数字只有 2 能转化为'2',是唯一确定的。所以虽然需要隐式转换,但不影响使用索引

小杨追问:“你还能在告诉我一些隐式转换的知识吗?”

我反手就是一个英文文档:

  1. If one or both arguments are NULL, the result of the comparison is NULLexcept for the NULL-safe <=> equality comparison operator. For NULL <=> NULL, the result is trueNo conversion is needed. 
  2.  
  3. If both arguments in a comparison operation are strings, they are compared as strings. 
  4.  
  5. If both arguments are integers, they are compared as integers. 
  6.  
  7. Hexadecimal values are treated as binary strings if not compared to a number. 
  8.  
  9. If one of the arguments is a TIMESTAMP or DATETIME column and the other argument is a constant, the constant is converted to a timestamp before the comparison is performed. This is done to be more ODBC-friendly. Note that this is not done for the arguments to IN()! To be safe, always use complete datetime, dateor time strings when doing comparisons. For example, to achieve best results when using BETWEEN with date or time values, use CAST() to explicitly convert the values to the desired data type. 
  10. A single-row subquery from a table or tables is not considered a constant. For example, if a subquery returns an integer to be compared to a DATETIME value, the comparison is done as two integers. The integer is not converted to a temporal value. To compare the operands as DATETIME values, use CAST() to explicitly convert the subquery value to DATETIME. 
  11.  
  12. If one of the arguments is a decimal value, comparison depends on the other argument. The arguments are compared as decimal values if the other argument is a decimal or integer value, or as floating-point values if the other argument is a floating-point value. 
  13.  
  14. In all other cases, the arguments are compared as floating-point (real) numbers. 

贴心的我帮你们翻译成了中文:

  1. 1, 两个参数至少有一个是 NULL 时,比较的结果也是 NULL,例外是使用 <=>  
  2. 对两个 NULL 做比较时会返回 1,这两种情况都不需要做类型转换 
  3.  
  4. 2, 两个参数都是字符串,会按照字符串来比较,不做类型转换 
  5.  
  6. 3, 两个参数都是整数,按照整数来比较,不做类型转换 
  7.  
  8. 4, 十六进制的值和非数字做比较时,会被当做二进制串 
  9.  
  10. 5, 有一个参数是 TIMESTAMP 或 DATETIME,并且另外一个参数是常量,常量会被转换为 timestamp 
  11.  
  12. 6, 有一个参数是 decimal 类型,如果另外一个参数是 decimal 或者整数会将整数转换为 decimal 后进行比较, 
  13.    如果另外一个参数是浮点数,则会把 decimal 转换为浮点数进行比较 
  14.  
  15. 7, 所有其他情况下,两个参数都会被转换为浮点数再进行比较 

再分享一个隐式转换的坑:你是否偶尔删除了一些不知道的数据?

  1. mysql> select * from test; 
  2. +----+-------+-----------+ 
  3. | id | name  | password  | 
  4. +----+-------+-----------+ 
  5. |  1 | test1 | password1 | 
  6. |  2 | test2 | password2 | 
  7. |  3 | aaa   | aaaa      | 
  8. |  4 | 55aaa | 55aaaa    | 
  9. |  5 | 1212  | aaa       | 
  10. |  6 | 1212a | aaa       | 
  11. +----+-------+-----------+ 
  12. rows in set (0.00 sec) 
  13.  
  14. mysql> select * from test where name = 1212; 
  15. +----+-------+----------+ 
  16. | id | name  | password | 
  17. +----+-------+----------+ 
  18. |  5 | 1212  | aaa      | 
  19. |  6 | 1212a | aaa      | 
  20. +----+-------+----------+ 
  21. rows in set, 5 warnings (0.00 sec) 
  22.  
  23. mysql> select * from test where name = '1212'
  24. +----+------+----------+ 
  25. | id | name | password | 
  26. +----+------+----------+ 
  27. |  5 | 1212 | aaa      | 
  28. +----+------+----------+ 
  29. 1 row in set (0.00 sec) 

上面的例子本意是查询 id 为 5 的那一条记录,结果把 id 为 6 的那一条也查询出来了。我想说明什么情况呢?

有时候我们的数据库表中的一些列是 varchar 类型,但是存储的值为‘1123’这种的纯数字的字符串值,一些同学写 SQL 的时候又不习惯加引号。

这样当进行 Select,Update或者 Delete 的时候就可能会多操作一些数据。所以应该加引号的地方别忘记了。

总而言之

隐式类型转换有无法命中索引的风险,在高并发、大数据量的情况下,命不中索引带来的后果可不止被运维和经理混合双打哦!且写 SQL 且 EXPLAIN!

作者:isysc1

编辑:陶家龙

出处:转载自微信公众号码儿嘟嘟骑(ID:maer_duduqi)

责任编辑:武晓燕 来源: 码儿嘟嘟骑
相关推荐

2020-02-03 09:10:23

数据库删库删库跑路

2024-02-20 14:40:35

Linux运维

2012-11-19 13:53:42

职场Google实习生

2013-06-07 09:59:40

Google实习面试

2010-10-12 11:06:07

招聘

2014-01-07 09:23:41

项目管理

2009-09-17 09:35:17

微软实习生

2013-11-26 14:15:43

2009-03-13 08:58:04

AOL裁员实习

2012-11-07 17:05:41

Google实习生

2011-12-07 20:37:42

iOSAndroid谷歌

2021-05-20 19:56:08

泄露密码数据泄露网络攻击

2015-04-14 15:05:35

Web前端开发腾讯暑期实习生

2024-01-09 15:51:56

Rust开发Trait

2013-02-20 10:40:21

实习生科技公司谷歌

2019-08-07 11:02:28

Python 开发编程语言

2020-06-10 10:21:33

机器狗人工智能波士顿

2015-06-29 10:56:16

程序员工资苹果

2020-12-09 11:38:16

数据库测试环境

2009-06-04 15:35:08

博克教育SAP行业实习生
点赞
收藏

51CTO技术栈公众号