varchar存儲規則
4.0版本以下,varchar(20),指的是20字節,如果存放UTF8漢字時,只能存6個(每個漢字3字節) 。
5.0版本以上,varchar(20),指的是20字符,無論存放的是數字、字母還是UTF8漢字(每個漢字3字節),都可以存放20個,最大大小是65532字節 。
Mysql4中最大也不過是20個字節,但是Mysql5根據編碼不同,存儲大小也不同。
varchar和char的區別
char是一種固定長度的類型,varchar則是一種可變長度的類型,它們的區別是: char(M)類型的數據列里,每個值都占用M個字節,如果某個長度小於M,MySQL就會在它的右邊用空格字符補足。(在檢索操作中那些填補出來的空格字符將被去掉)在varchar(M)類型的數據列里,每個值只占用剛好夠用的字節再加上一個用來記錄其長度的字節(即總長度為L+1字節)。
在MySQL中用來判斷是否需要進行對數據列類型轉換的規則:
1、在一個數據表里,如果每一個數據列的長度都是固定的,那么每一個數據行的長度也將是固定的。
2、只要數據表里有一個數據列的長度的可變的,那么每個數據行的長度都是可變的。
3、如果某個數據表里的數據行的長度是可變的,那么,為了節約存儲空間,MySQL會把這個數據表里的固定長度類型的數據列轉換為相應的可變長度類型。例外:長度小於4個字符的char數據列不會被轉換為varchar類型。
MySQL中varchar最大長度是多少?
這不是一個固定的數字。本文簡要說明一下限制規則。
限制規則
a) 存儲限制
varchar 字段是將實際內容單獨存儲在聚簇索引之外,內容開頭用1到2個字節表示實際長度(長度超過255時需要2個字節),因此最大長度不能超過65535。
b) 編碼長度限制
字符類型若為gbk,每個字符最多占2個字節,最大長度不能超過32766。
字符類型若為utf8,每個字符最多占3個字節,最大長度不能超過21845。
若定義的時候超過上述限制,則varchar字段會被強行轉為text類型,並產生warning。
c) 行長度限制
導致實際應用中varchar長度限制的是一個行定義的長度。 MySQL要求一個行的定義長度不能超過65535。若定義的表長度超過這個值,則提示
ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. You have to change some columns to TEXT or BLOBs。
計算示例
舉兩個例說明一下實際長度的計算。
a) 若一個表只有一個varchar類型,如定義為
create table t4(c varchar(N)) charset=gbk;
則此處N的最大值為(65535-1-2)/2= 32766。
減1的原因是實際行存儲從第二個字節開始’;
減2的原因是varchar頭部的2個字節表示長度;
除2的原因是字符編碼是gbk。
b) 若一個表定義為
create table t4(c int, c2 char(30), c3 varchar(N)) charset=utf8;
則此處N的最大值為 (65535-1-2-4-30*3)/3=21812
減1和減2與上例相同;
減4的原因是int類型的c占4個字節;
減30*3的原因是char(30)占用90個字節,編碼是utf8。
如果被varchar超過上述的b規則,被強轉成text類型,則每個字段占用定義長度為11字節,當然這已經不是“varchar”了。
則此處N的最大值為 (65535-1-2-4-30*3)/3=21812
create table t4(c int, c2 char(30), c3 varchar(21812)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
create table t5(c int, c2 varchar(30), c3 varchar(21812)) ENGINE=InnoDB DEFAULT CHARSET=utf8
varchar(30)和char(30)最多能存放:
工在基工左基工在基順工作奔大規模集成電路城工作東奔西走左奪工城韉革城載模壓地工魂牽夢縈栽土木工程魂牽夢縈栽植奇巧魂牽夢縈地廳城柑模壓東奔西走苦村落模壓革革柑可耕地村模壓基栽魂牽夢基
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
CREATE TABLE `t` (`var` varchar(21844) default NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8
可以正常的執行。
CREATE TABLE `t` (`var` varchar(21845) default NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8
[Err] 1118 - Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. You have to change some columns to TEXT or BLOBs
執行有錯誤。
實戰
這里有一道關於MySQL的題:
表設計如下
column name | type |
---|---|
id | mediumint |
name | varchar |
問題是: MySQL5.1 , 在GBK字符環境下,這里的varchar最長能設多長?
正確答案是: 32764
那如果表設計為兩個 varchar, 第一個長度設為300時,第二個varchar應該多長? --答案見本文最后面
要想搞明白是怎么算出來的,請仔細下面的解釋:
官方文檔上對 int 及 varchar 的說明如下:
Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 255 before MySQL 5.0.3, and 0 to 65,535 in 5.0.3 and later versions.
看到這里,你以為上面的varchar長度是: 65535 /2=32767?
不對,因為官方文檔后面又說:
In contrast to CHAR, VARCHAR values are stored as a one-byte or two-byte length prefix plus data. The length prefix indicates the number of bytes in the value.
很明白了,varchar會保留一至兩個字節來存放長度信息,但到底是1Byte還是2Byte?
往后看:
A column uses one length byte if values require no more than 255 bytes, two length bytes if values may require more than 255 bytes.
你再來算一算varchar長度 最大為: (65535-2) /2 = 32766 ?
還是不對!
其實每一行的總長度是有限制的,即最大為65535.
Every table has a maximum row size of 65,535 bytes.
This maximum applies to all storage engines, but a given engine might have additional constraints that result in a lower effective maximum row size.
所以算varchar得把id的扣除:
(65535-3-2) /2 = 32765 ?
測試一下:
root@saker 05:27:28>create table t4(id mediumint, name varchar(32765));
ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. You have to change some columns to TEXT or BLOBs
用32764測試一下:
root@saker 05:27:31>create table t4(id mediumint, name varchar(32764));
Query OK, 0 rows affected (0.16 sec)
root@saker 05:27:35>desc t4;
+-------+----------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------------+------+-----+---------+-------+
| id | mediumint(9) | YES | | NULL | |
| name | varchar(32764) | YES | | NULL | |
+-------+----------------+------+-----+---------+-------+
這說明還保留了1至2個字節來留其它控制信息。(在文檔里面我沒有找到說明,所以也不知道到底是1個還是2個byte)但我想到了一個方法來反推出來,我把id的類型從medium改成 int ,這時id的長度就從3變為4了,如果控制字節用了2Bytes,那varchar的長度還設為32764的話,顯然是要報錯的。。。
root@saker 05:29:14>create table t3(id int, name varchar(32764));
Query OK, 0 rows affected (0.18 sec)
root@saker 07:01:10>desc t3;
+-------+----------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(32764) | YES | | NULL | |
+-------+----------------+------+-----+---------+-------+
這樣應該能說明,控制位只有1個字節。
下面用這個結論來計算第二個問題:
那如果表設計為兩個 varchar, 第一個長度設為300時,第二個varchar應該多長?
(65535-1-2-2-300*2) /2 = 32465
注: 1表示控制位占用的一個字節,兩個2表示兩個varchar的長度信息字節(因為兩個varchar的長度都超過了255字節,所以長度信息字節都為2個字節),300*2表示varchar(300)在gbk編碼下占用字節總數。
測試一下:
root@saker 07:09:23>create table t1(id varchar(300), name varchar(32465));
Query OK, 0 rows affected (0.15 sec)
root@saker 07:09:26>desc t1;
+-------+----------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------------+------+-----+---------+-------+
| id | varchar(300) | YES | | NULL | |
| name | varchar(32465) | YES | | NULL | |
+-------+----------------+------+-----+---------+-------+
2 rows in set (0.02 sec)
多一位都不行:
root@saker 07:08:12>create table t1(id varchar(300), name varchar(32466));
ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. You have to change some columns to TEXT or BLOBs
最后 把以上所有知識綜合起來:
有一個表:
column name | type |
---|---|
id | int |
name1 | char(20) |
name2 | varchar(100) |
name3 | varchar(? ? ?) |
算一下:
(65535-1-1-2-4-20*2-100*2) /2 = 32643
注:一個1表示控制位占用的一個字節,一個1表示name2的保存varchar長度信息占用的一個字節,2表示name3的varchar長度信息占用的兩個字節,4表示id的int類型占用的四個字節,20*2表示name1的char類型在gbk編碼下最多占用的40個字節,100*2表示name2在gbk編碼下占用的字節數。
root@saker 07:16:09>create table tt(id int, name1 char(20), name2 varchar(100), name3 varchar(32643));
Query OK, 0 rows affected (0.18 sec)
root@saker 07:16:12>desc tt;
+-------+----------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name1 | char(20) | YES | | NULL | |
| name2 | varchar(100) | YES | | NULL | |
| name3 | varchar(32643) | YES | | NULL | |
+-------+----------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
root@saker 07:15:08>create table tt(id int, name1 char(20), name2 varchar(100), name3 varchar(32644));
ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. You have to change some columns to TEXT or BLOBs
所以設計表的時候,這個要注意了,每一行是有長度限制的。
Mysql數據類型占用字節數(摘自Mysql官網)
Numeric Type
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 |
Date and Time Type
For TIME, DATETIME, and TIMESTAMP columns, the storage required for tables created before MySQL 5.6.4 differs from tables created from 5.6.4 on. This is due to a change in 5.6.4 that permits these types to have a fractional part, which requires from 0 to 3 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 |
String Type
In the following table, M represents the declared column length in characters for nonbinary string types and bytes for binary string types. L represents the actual length in bytes of a given string value.
Data Type | Storage Required |
---|---|
CHAR(M) | The compact family of InnoDB row formats optimize storage for variable-length character sets. See COMPACT Row Format Storage Characteristics. Otherwise, M ×w bytes, <= 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) |