1. 程式人生 > >(轉)MySQL之char、varchar和text的設計

(轉)MySQL之char、varchar和text的設計

SQ 數值 abc dep 添加 pos sage character AC

from:

http://www.cnblogs.com/billyxp/p/3548540.html

最近有表結構設計中出現了varchar(10000)的設計引起了大家的討論,我們下面就來分析分析。

首先我們先普及一下常識:

1、char(n)和varchar(n)中括號中n代表字符的個數,並不代表字節個數,所以當使用了中文的時候(UTF8)意味著可以插入m個中文,但是實際會占用m*3個字節。

2、同時char和varchar最大的區別就在於char不管實際value都會占用n個字符的空間,而varchar只會占用實際字符應該占用的空間+1,並且實際空間+1<=n。

3、超過char和varchar的n設置後,字符串會被截斷。

4、char的上限為255字節,varchar的上限65535字節,text的上限為65535。

5、char在存儲的時候會截斷尾部的空格,varchar和text不會。

6、varchar會使用1-3個字節來存儲長度,text不會。

下圖可以非常明顯的看到結果:

Value

CHAR(4)

Storage Required

VARCHAR(4)

Storage Required

‘‘

‘ ‘

4 bytes

‘‘

1 byte

‘ab‘

‘ab ‘

4 bytes

‘ab‘

3 bytes

‘abcd‘

‘abcd‘

4 bytes

‘abcd‘

5 bytes

‘abcdefgh‘

‘abcd‘

4 bytes

‘abcd‘

5 bytes

總體來說:

1、char,存定長,速度快,存在空間浪費的可能,會處理尾部空格,上限255。

2、varchar,存變長,速度慢,不存在空間浪費,不處理尾部空格,上限65535,但是有存儲長度實際65532最大可用。

3、text,存變長大數據,速度慢,不存在空間浪費,不處理尾部空格,上限65535,會用額外空間存放數據長度,顧可以全部使用65535。

接下來,我們說說這個場景的問題:

當varchar(n)後面的n非常大的時候我們是使用varchar好,還是text好呢?這是個明顯的量變引發質變的問題。我們從2個方面考慮,第一是空間,第二是性能。

首先從空間方面:

從官方文檔中我們可以得知當varchar大於某些數值的時候,其會自動轉換為text,大概規則如下:

    • 大於varchar(255)變為 tinytext
    • 大於varchar(500)變為 text
    • 大於varchar(20000)變為 mediumtext

所以對於過大的內容使用varchar和text沒有太多區別。

其次從性能方面:

索引會是影響性能的最關鍵因素,而對於text來說,只能添加前綴索引,並且前綴索引最大只能達到1000字節。

而貌似varhcar可以添加全部索引,但是經過測試,其實也不是。由於會進行內部的轉換,所以long varchar其實也只能添加1000字節的索引,如果超長了會自動截斷。

技術分享圖片
localhost.test>create table test (a varchar(1500));
Query OK, 0 rows affected (0.01 sec)

localhost.test>alter table test add index idx_a(a);
Query OK, 0 rows affected, 2 warnings (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 2

localhost.test>show warnings;
+---------+------+---------------------------------------------------------+
| Level   | Code | Message                                                 |
+---------+------+---------------------------------------------------------+
| Warning | 1071 | Specified key was too long; max key length is 767 bytes |
| Warning | 1071 | Specified key was too long; max key length is 767 bytes |
+---------+------+---------------------------------------------------------+
技術分享圖片

從上面可以明顯單看到索引被截斷了。而這個767是怎麽回事呢?這是由於innodb自身的問題,使用innodb_large_prefix設置。

從索引上看其實long varchar和text也沒有太多區別。

所以我們認為當超過255的長度之後,使用varchar和text沒有本質區別,只需要考慮一下兩個類型的特性即可。(主要考慮text沒有默認值的問題)

技術分享圖片
CREATE TABLE `test` (
  `id` int(11) DEFAULT NULL,
  `a` varchar(500) DEFAULT NULL,
  `b` text
) ENGINE=InnoDB DEFAULT CHARSET=utf8

+----------+------------+-----------------------------------+
| Query_ID | Duration   | Query                             |
+----------+------------+-----------------------------------+
|        1 | 0.01513200 | select a from test where id=10000 |
|        2 | 0.01384500 | select b from test where id=10000 |
|        3 | 0.01124300 | select a from test where id=15000 |
|        4 | 0.01971600 | select b from test where id=15000 |
+----------+------------+-----------------------------------+
技術分享圖片

從上面的簡單測試看,基本上是沒有什麽區別的,但是個人推薦使用varchar(10000),畢竟這個還有截斷,可以保證字段的最大值可控,如果使用text那麽如果code有漏洞很有可能就寫入數據庫一個很大的內容,會造成風險。

故,本著short is better原則,還是使用varchar根據需求來限制最大上限最好。

附錄:各個字段類型的存儲需求

Data Type

Storage Required

TINYINT

1 byte

SMALLINT

2 bytes

MEDIUMINT

3 bytes

INT, INTEGER

4 bytes

BIGINT

8 bytes

FLOAT(p)

4 bytes if 0 <= p <= 24, 8 bytes if 25 <= p <= 53

FLOAT

4 bytes

DOUBLE [PRECISION], REAL

8 bytes

DECIMAL(M,D), NUMERIC(M,D)

Varies; see following discussion

BIT(M)

approximately (M+7)/8 bytes

Data Type

Storage Required Before MySQL 5.6.4

Storage Required as of MySQL 5.6.4

YEAR

1 byte

1 byte

DATE

3 bytes

3 bytes

TIME

3 bytes

3 bytes + fractional seconds storage

DATETIME

8 bytes

5 bytes + fractional seconds storage

TIMESTAMP

4 bytes

4 bytes + fractional seconds storage

Data Type

Storage Required

CHAR(M)

M × w bytes, 0 <= M <= 255, where w is the number of bytes required for the maximum-length character in the character set

BINARY(M)

M bytes, 0 < = M <= 255

VARCHAR(M), VARBINARY(M)

L + 1 bytes if column values require 0 – 255 bytes, L + 2 bytes if values may require more than 255 bytes

TINYBLOB, TINYTEXT

L + 1 bytes, where L < 28

BLOB, TEXT

L + 2 bytes, where L < 216

MEDIUMBLOB, MEDIUMTEXT

L + 3 bytes, where L < 224

LONGBLOB, LONGTEXT

L + 4 bytes, where L < 232

ENUM(‘value1‘,‘value2‘,...)

1 or 2 bytes, depending on the number of enumeration values (65,535 values maximum)

SET(‘value1‘,‘value2‘,...)

1, 2, 3, 4, or 8 bytes, depending on the number of set members (64 members maximum)

(轉)MySQL之char、varchar和text的設計