必須要注意:
不同的Mysql儲存引擎在具體實現不同的資料類型時,儲存方式、空間需求、處理方式都不一定完全相同。所以,這個問題必須放在具體的儲存引擎實現上才有意義!
另外,即使是同一儲存引擎,版本不同,儲存實現 也可能有變化!
以下討論限於通常的mysql的 myisam儲存引擎。
Storage Requirements for Numeric Types in MyISAM
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。大概為:(有效數字個數M/9)*4 個byte |
BIT(M ) |
approximately (M +7)/8 bytes。大概為:(容量M+7)/8個字元,通常為:1個位元組 |
Values for DECIMAL
(and NUMERIC
) columns are represented using a binary format that packs nine decimal (base 10) digits into four bytes. Storage for the integer and fractional parts of each value are determined separately. Each multiple of nine digits requires four bytes, and the “leftover” digits require some fraction of four bytes. The storage required for excess digits is given by the following table.
Leftover Digits |
Number of Bytes |
0 |
0 |
1 |
1 |
2 |
1 |
3 |
2 |
4 |
2 |
5 |
3 |
6 |
3 |
7 |
4 |
8 |
4 |
Storage Requirements for Date and Time Types in MyISAM
Data Type |
Storage Required |
DATE |
3 bytes |
TIME |
3 bytes |
DATETIME |
8 bytes |
TIMESTAMP |
4 bytes |
YEAR |
1 byte |
The storage requirements shown in the table arise from the way that MySQL represents temporal values:
DATE
: A three-byte integer packed as DD
+ MM
×32 + YYYY
×16×32
TIME
: A three-byte integer packed as DD
×24×3600 + HH
×3600 + MM
×60 + SS
DATETIME
: Eight bytes:
TIMESTAMP
: A four-byte integer representing seconds UTC since the epoch ('1970-01-01 00:00:00'
UTC)
YEAR
: A one-byte integer
Storage Requirements for String Types in MyISAM
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 ) |
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, where L < 28, L + 2 bytes, where L < 216 |
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) |
Variable-length string types are stored using a length prefix plus data. The length prefix requires from one to four bytes depending on the data type, and the value of the prefix is L
(the byte length of the string). For example, storage for a MEDIUMTEXT
value requires L
bytes to store the value plus three bytes to store the length of the value.
To calculate the number of bytes used to store a particular CHAR
, VARCHAR
, or TEXT
column value, you must take into account the character set used for that column and whether the value contains multi-byte characters. In particular, when using the utf8
Unicode character set, you must keep in mind that not all utf8
characters use the same number of bytes and can require up to three bytes per character. For a breakdown of the storage used for different categories of utf8
characters, see Section 9.1.10, “Unicode Support”.
VARCHAR
, VARBINARY
, and the BLOB
and TEXT
types are variable-length types. For each, the storage requirements depend on these factors:
The actual length of the column value
The column's maximum possible length
The character set used for the column, because some character sets contain multi-byte characters
For example, a VARCHAR(255)
column can hold a string with a maximum length of 255 characters. Assuming that the column uses the latin1
character set (one byte per character), the actual storage required is the length of the string (L
), plus one byte to record the length of the string. For the string 'abcd'
, L
is 4 and the storage requirement is five bytes. If the same column is instead declared to use the ucs2
double-byte character set, the storage requirement is 10 bytes: The length of 'abcd'
is eight bytes and the column requires two bytes to store lengths because the maximum length is greater than 255 (up to 510 bytes).
Note
The effective maximum number of bytes that can be stored in a VARCHAR
or VARBINARY
column is subject to the maximum row size of 65,535 bytes, which is shared among all columns. For a VARCHAR
column that stores multi-byte characters, the effective maximum number of characters is less. For example, utf8
characters can require up to three bytes per character, so a VARCHAR
column that uses the utf8
character set can be declared to be a maximum of 21,844 characters.