歡迎您光臨本站 註冊首頁

mysql 中對於整型的一個小注意事項

←手機掃碼閱讀     火星人 @ 2014-03-03 , reply:0

mysql 中對於整型的一個小注意事項

mysql 中對於整型的一個小注意事項


大家都知道,在我們平時的查詢中,通過主鍵in查詢,是很快的,而由於我們的程序出現bug,導致in一個超級大的整數時,超過了整數範圍時候,會出現什麼情況呢?(這也是一次故障的教訓)
看如下語句:
CREATE TABLE `sbtest` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `k` int(10) unsigned NOT NULL DEFAULT '0',
  `c` char(120) NOT NULL DEFAULT '',
  `pad` char(60) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=951020001

1、
mysql> explain select * from sbtest where id in (1,100,429496729800)\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: sbtest
         type: range
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 4
          ref: NULL
         rows: 3
        Extra: Using where
1 row in set (0.00 sec)
2、而我們使用BIGINT無符號類型的最大值(18446744073709551615)時:
mysql> explain select * from sbtest where id in (100,951000,18446744073709551615)\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: sbtest
        type: range
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 4
          ref: NULL
         rows: 3
        Extra: Using where
1 row in set (0.00 sec)
3、當我們使用比 BIGINT無符號類型的最大值大1時,確出現了神奇的問題,掃描整個表了,如下:
mysql> explain select * from sbtest where id in (100,951000,18446744073709551616)\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: sbtest
         type: ALL
possible_keys: PRIMARY
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 951020065
        Extra: Using where
1 row in set (0.00 sec)

所以,我們一定要小心小心在小心,不要因為程序的bug,而觸發mysql的bug(其實也不能算),
這可是一次血的教訓,開發人員生成了一個N長串,然後update where id in (長串),導致了最終結果:整個表被鎖-->連接數爆滿-->資料庫崩潰!

[火星人 ] mysql 中對於整型的一個小注意事項已經有363次圍觀

http://coctec.com/docs/service/show-post-2260.html