成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

新來的實(shí)習(xí)生把數(shù)據(jù)庫搞炸了......

運(yùn)維 數(shù)據(jù)庫運(yùn)維
SQL 寫的秒,漲薪呱呱叫!就在前不久公司新來的實(shí)習(xí)生因?yàn)閷戝e(cuò)了一條SQL把數(shù)據(jù)庫搞炸了。

 SQL 寫的秒,漲薪呱呱叫!就在前不久公司新來的實(shí)習(xí)生因?yàn)閷戝e(cuò)了一條SQL把數(shù)據(jù)庫搞炸了。

[[334989]]

 

圖片來自 Pexels

新來的實(shí)習(xí)生小楊寫了一條 SQL 語句:

  1. SELECT wx_id from `userWHERE wx_id = 2 

當(dāng)小楊迫不及待準(zhǔn)備下班回家的時(shí)候,隔壁的王經(jīng)理一把抓住了小楊,并用 EXPLAIN 命令教育了小楊,小楊流下了沒有文化的淚水。

這條 SQL 語句中,wx_id 是具有索引的,但是王經(jīng)理查出來的結(jié)果卻是這樣的:

 

王經(jīng)理的教育

小楊仔細(xì)一瞅 key 字段顯示為 Null,很明顯這條 SQL 語句沒有走索引。

小楊心想“糟糕,又寫錯(cuò) SQL 語句了,這下又要面臨運(yùn)維和經(jīng)理的混合雙打了, 不行我得立馬改下這條 SQL 語句,讓我想想哪里出錯(cuò)了”!

[[334990]]

 

小楊腦袋瓜瘋狂亂撞,仔細(xì)回想表結(jié)構(gòu),忽然想到,wx_id 字段是 varchar 類型,自己查詢的時(shí)候竟然沒有加引號。

小楊一把搶過經(jīng)理手里的鍵盤,往 wx_id 的查詢條件上加了引號,結(jié)果:

果然這條 SQL 語句開始走了索引。小楊沾沾自喜以為解決了個(gè)天大的 Bug。

 

經(jīng)理微微一笑問道“你知道為什么為什么加了引號就走了索引嗎?如果字段是 int 類型,那么查詢的時(shí)候需不需要加引號呢?又是為什么呢?”

正餐來了

小楊被問的呆在原地,無法回答。

經(jīng)過小楊研究發(fā)現(xiàn),如果字段是 varchar類型,等號右側(cè)必須加引號才走索引;如果字段是 int 類型,那么等號右側(cè)加不加引號都是會(huì)走索引的。

什么?你不相信小楊說的話,有圖有真相。(bonus 字段類型為int)

 

真相圖

但是結(jié)論出來,還是無法回答經(jīng)理的奪命三連問。

小楊搬來了答案

在 MySQL 查詢中,當(dāng)查詢條件左右兩側(cè)類型不匹配的時(shí)候會(huì)發(fā)生隱式轉(zhuǎn)換:

  1. 也就是說 
  2. SELECT wx_id from `userWHERE wx_id = 2 
  3. 等價(jià)于 
  4. SELECT wx_id from `userWHERE CAST(wx_id AS signed int) = 2 

一旦對索引字段做函數(shù)操作,MySQL 會(huì)放棄使用索引。

所以如果字段是 varchar 類型,等號右側(cè)必須加引號才走索引,否則由于隱式轉(zhuǎn)換,MySQL 會(huì)放棄使用索引。那么憑什么 int 加不加引號都可以使用索引呢?

那是因?yàn)?int 類型的數(shù)字只有 2 能轉(zhuǎn)化為'2',是唯一確定的。所以雖然需要隱式轉(zhuǎn)換,但不影響使用索引

小楊追問:“你還能在告訴我一些隱式轉(zhuǎn)換的知識(shí)嗎?”

我反手就是一個(gè)英文文檔:

  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. 

貼心的我?guī)湍銈兎g成了中文:

  1. 1, 兩個(gè)參數(shù)至少有一個(gè)是 NULL 時(shí),比較的結(jié)果也是 NULL,例外是使用 <=>  
  2. 對兩個(gè) NULL 做比較時(shí)會(huì)返回 1,這兩種情況都不需要做類型轉(zhuǎn)換 
  3.  
  4. 2, 兩個(gè)參數(shù)都是字符串,會(huì)按照字符串來比較,不做類型轉(zhuǎn)換 
  5.  
  6. 3, 兩個(gè)參數(shù)都是整數(shù),按照整數(shù)來比較,不做類型轉(zhuǎn)換 
  7.  
  8. 4, 十六進(jìn)制的值和非數(shù)字做比較時(shí),會(huì)被當(dāng)做二進(jìn)制串 
  9.  
  10. 5, 有一個(gè)參數(shù)是 TIMESTAMP 或 DATETIME,并且另外一個(gè)參數(shù)是常量,常量會(huì)被轉(zhuǎn)換為 timestamp 
  11.  
  12. 6, 有一個(gè)參數(shù)是 decimal 類型,如果另外一個(gè)參數(shù)是 decimal 或者整數(shù)會(huì)將整數(shù)轉(zhuǎn)換為 decimal 后進(jìn)行比較, 
  13.    如果另外一個(gè)參數(shù)是浮點(diǎn)數(shù),則會(huì)把 decimal 轉(zhuǎn)換為浮點(diǎn)數(shù)進(jìn)行比較 
  14.  
  15. 7, 所有其他情況下,兩個(gè)參數(shù)都會(huì)被轉(zhuǎn)換為浮點(diǎn)數(shù)再進(jìn)行比較 

再分享一個(gè)隱式轉(zhuǎn)換的坑:你是否偶爾刪除了一些不知道的數(shù)據(jù)?

  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 的那一條記錄,結(jié)果把 id 為 6 的那一條也查詢出來了。我想說明什么情況呢?

有時(shí)候我們的數(shù)據(jù)庫表中的一些列是 varchar 類型,但是存儲(chǔ)的值為‘1123’這種的純數(shù)字的字符串值,一些同學(xué)寫 SQL 的時(shí)候又不習(xí)慣加引號。

這樣當(dāng)進(jìn)行 Select,Update或者 Delete 的時(shí)候就可能會(huì)多操作一些數(shù)據(jù)。所以應(yīng)該加引號的地方別忘記了。

總而言之

隱式類型轉(zhuǎn)換有無法命中索引的風(fēng)險(xiǎn),在高并發(fā)、大數(shù)據(jù)量的情況下,命不中索引帶來的后果可不止被運(yùn)維和經(jīng)理混合雙打哦!且寫 SQL 且 EXPLAIN!

作者:isysc1

編輯:陶家龍

出處:轉(zhuǎn)載自微信公眾號碼兒嘟嘟騎(ID:maer_duduqi)

責(zé)任編輯:武曉燕 來源: 碼兒嘟嘟騎
相關(guān)推薦

2020-02-03 09:10:23

數(shù)據(jù)庫刪庫刪庫跑路

2024-02-20 14:40:35

Linux運(yùn)維

2024-11-19 08:36:16

2012-11-19 13:53:42

職場Google實(shí)習(xí)生

2024-05-27 00:30:00

2010-10-12 11:06:07

招聘

2013-06-07 09:59:40

Google實(shí)習(xí)面試

2014-01-07 09:23:41

項(xiàng)目管理

2009-09-17 09:35:17

微軟實(shí)習(xí)生

2012-11-07 17:05:41

Google實(shí)習(xí)生

2013-11-26 14:15:43

2024-01-09 15:51:56

Rust開發(fā)Trait

2011-12-07 20:37:42

iOSAndroid谷歌

2021-05-20 19:56:08

泄露密碼數(shù)據(jù)泄露網(wǎng)絡(luò)攻擊

2015-04-14 15:05:35

Web前端開發(fā)騰訊暑期實(shí)習(xí)生

2009-03-13 08:58:04

AOL裁員實(shí)習(xí)

2019-08-07 11:02:28

Python 開發(fā)編程語言

2013-02-20 10:40:21

實(shí)習(xí)生科技公司谷歌

2020-06-10 10:21:33

機(jī)器狗人工智能波士頓

2009-06-23 08:56:03

跨國公司大三實(shí)習(xí)生
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 欧美99 | 亚洲精品中文字幕中文字幕 | 狠狠综合久久av一区二区小说 | 久久精品无码一区二区三区 | 在线免费视频一区 | 久久久91精品国产一区二区三区 | 在线视频91 | 成人依人 | 久久久久久久久久毛片 | 国产乱码精品一区二三赶尸艳谈 | 欧美精品影院 | 成人福利网 | 欧美日韩一区不卡 | 亚洲视频自拍 | 亚洲综合激情 | 精品久久久久香蕉网 | 国产成人久久精品一区二区三区 | 欧美精品在线看 | 亚洲精品高清视频在线观看 | 亚洲精品中文字幕在线 | 一级大片| 亚洲一区二区三区桃乃木香奈 | 无码一区二区三区视频 | 国产精品一区二区三区在线 | 亚洲精品一区二区三区 | 久久久免费 | 日本高清视频在线播放 | 丁香婷婷久久久综合精品国产 | 久久久久一区二区三区四区 | 久久看片| 伊人青青久久 | 国产一区二区视频免费在线观看 | 中文字幕一区二区三区四区五区 | av一区在线观看 | 一区二区三区欧美 | 亚洲瑟瑟| 日韩网站在线观看 | 国产精品毛片一区二区在线看 | 午夜资源| 日韩1区 | 九九精品网 |