MySQL 数据类型字节长度

555 字
3 分钟
MySQL 数据类型字节长度

Numeric Type Storage Requirements#

Data TypeStorage Required
TINYINT1 byte
SMALLINT2 bytes
MEDIUMINT3 bytes
INTINTEGER4 bytes
BIGINT8 bytes
FLOAT(``***p***``)4 bytes if 0 <= ***p*** <= 24, 8 bytes if 25 <= ***p*** <= 53
FLOAT4 bytes
DOUBLE [PRECISION]REAL8 bytes
DECIMAL(``***M***``,``***D***``)NUMERIC(``***M***``,``***D***``)Varies; see following discussion
BIT(``***M***``)approximately (***M***+7)/8 bytes

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 DigitsNumber of Bytes
00
11
21
32
42
53
63
74
84

Date and Time Type Storage Requirements#

For TIMEDATETIME, 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 TypeStorage Required Before MySQL 5.6.4Storage Required as of MySQL 5.6.4
**YEAR**1 byte1 byte
**DATE**3 bytes3 bytes
**TIME**3 bytes3 bytes + fractional seconds storage
**DATETIME**8 bytes5 bytes + fractional seconds storage
**TIMESTAMP**4 bytes4 bytes + fractional seconds storage

As of MySQL 5.6.4, storage for YEAR and DATE remains unchanged. However, TIMEDATETIME, and TIMESTAMP are represented differently. DATETIME is packed more efficiently, requiring 5 rather than 8 bytes for the nonfractional part, and all three parts have a fractional part that requires from 0 to 3 bytes, depending on the fractional seconds precision of stored values.

Fractional Seconds PrecisionStorage Required
00 bytes
1, 21 byte
3, 42 bytes
5, 63 bytes

For example, TIME(0)TIME(2)TIME(4), and TIME(6) use 3, 4, 5, and 6 bytes, respectively. TIME and TIME(0) are equivalent and require the same storage.

String Type Storage Requirements#

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 TypeStorage 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
TINYBLOBTINYTEXT***L*** + 1 bytes, where ***L*** < 28
BLOBTEXT***L*** + 2 bytes, where ***L*** < 216
MEDIUMBLOBMEDIUMTEXT***L*** + 3 bytes, where ***L*** < 224
LONGBLOBLONGTEXT***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)

Spatial Type Storage Requirements#

MySQL stores geometry values using 4 bytes to indicate the SRID followed by the WKB representation of the value. The LENGTH() function returns the space in bytes required for value storage.

JSON Storage Requirements#

In general, the storage requirement for a JSON column is approximately the same as for a LONGBLOB or LONGTEXT column; that is, the space consumed by a JSON document is roughly the same as it would be for the document’s string representation stored in a column of one of these types. However, there is an overhead imposed by the binary encoding, including metadata and dictionaries needed for lookup, of the individual values stored in the JSON document. For example, a string stored in a JSON document requires 4 to 10 bytes additional storage, depending on the length of the string and the size of the object or array in which it is stored.

In addition, MySQL imposes a limit on the size of any JSON document stored in a JSON column such that it cannot be any larger than the value of max_allowed_packet.

📎 参考文章#

文章分享

如果这篇文章对你有帮助,欢迎分享给更多人!

MySQL 数据类型字节长度
https://blog.sephy.top/posts/mysql-数据类型字节长度/
作者
虾米
发布于
2024-02-26
许可协议
CC BY-NC-SA 4.0
Profile Image of the Author
虾米
coder
分类
标签
站点统计
文章
61
分类
4
标签
52
总字数
64,663
运行时长
0
最后活动
0 天前
站点信息
构建平台
GitHub Actions
博客版本
Firefly v6.13.9
文章许可
CC BY-NC-SA 4.0