Documentation Home
MySQL 8.0 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 43.3Mb
PDF (A4) - 43.4Mb
Man Pages (TGZ) - 297.3Kb
Man Pages (Zip) - 402.5Kb
Info (Gzip) - 4.3Mb
Info (Zip) - 4.3Mb
Excerpts from this Manual

MySQL 8.0 Reference Manual  / Data Types  /  Data Type Storage Requirements

13.7 Data Type Storage Requirements

The storage requirements for table data on disk depend on several factors. Different storage engines represent data types and store raw data differently. Table data might be compressed, either for a column or an entire row, complicating the calculation of storage requirements for a table or column.

Despite differences in storage layout on disk, the internal MySQL APIs that communicate and exchange information about table rows use a consistent data structure that applies across all storage engines.

This section includes guidelines and information for the storage requirements for each data type supported by MySQL, including the internal format and size for storage engines that use a fixed-size representation for data types. Information is listed by category or storage engine.

The internal representation of a table has a maximum row size of 65,535 bytes, even if the storage engine is capable of supporting larger rows. This figure excludesBLOB orTEXT columns, which contribute only 9 to 12 bytes toward this size. ForBLOB andTEXT data, the information is stored internally in a different area of memory than the row buffer. Different storage engines handle the allocation and storage of this data in different ways, according to the method they use for handling the corresponding types. For more information, seeChapter 18,Alternative Storage Engines, andSection 10.4.7, “Limits on Table Column Count and Row Size”.

InnoDB Table Storage Requirements

SeeSection 17.10, “InnoDB Row Formats” for information about storage requirements forInnoDB tables.

NDB Table Storage Requirements

Important

NDB tables use4-byte alignment; allNDB data storage is done in multiples of 4 bytes. Thus, a column value that would typically take 15 bytes requires 16 bytes in anNDB table. For example, inNDB tables, theTINYINT,SMALLINT,MEDIUMINT, andINTEGER (INT) column types each require 4 bytes storage per record due to the alignment factor.

EachBIT(M) column takesM bits of storage space. Although an individualBIT column isnot 4-byte aligned,NDB reserves 4 bytes (32 bits) per row for the first 1-32 bits needed forBIT columns, then another 4 bytes for bits 33-64, and so on.

While aNULL itself does not require any storage space,NDB reserves 4 bytes per row if the table definition contains any columns allowingNULL, up to 32NULL columns. (If an NDB Cluster table is defined with more than 32NULL columns up to 64NULL columns, then 8 bytes per row are reserved.)

Every table using theNDB storage engine requires a primary key; if you do not define a primary key, ahidden primary key is created byNDB. This hidden primary key consumes 31-35 bytes per table record.

You can use thendb_size.pl Perl script to estimateNDB storage requirements. It connects to a current MySQL (not NDB Cluster) database and creates a report on how much space that database would require if it used theNDB storage engine. SeeSection 25.5.28, “ndb_size.pl — NDBCLUSTER Size Requirement Estimator” for more information.

Numeric Type Storage Requirements

Data TypeStorage Required
TINYINT1 byte
SMALLINT2 bytes
MEDIUMINT3 bytes
INT,INTEGER4 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 forDECIMAL (andNUMERIC) 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 theleftover 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

ForTIME,DATETIME, andTIMESTAMP 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
YEAR1 byte1 byte
DATE3 bytes3 bytes
TIME3 bytes3 bytes + fractional seconds storage
DATETIME8 bytes5 bytes + fractional seconds storage
TIMESTAMP4 bytes4 bytes + fractional seconds storage

As of MySQL 5.6.4, storage forYEAR andDATE remains unchanged. However,TIME,DATETIME, andTIMESTAMP 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), andTIME(6) use 3, 4, 5, and 6 bytes, respectively.TIME andTIME(0) are equivalent and require the same storage.

For details about internal representation of temporal values, seeMySQL Internals: Important Algorithms and Structures.

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. SeeCOMPACT Row Format Storage Characteristics. Otherwise,M ×w bytes,<=M <= 255, wherew 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,TINYTEXTL + 1 bytes, whereL < 28
BLOB,TEXTL + 2 bytes, whereL < 216
MEDIUMBLOB,MEDIUMTEXTL + 3 bytes, whereL < 224
LONGBLOB,LONGTEXTL + 4 bytes, whereL < 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 isL (the byte length of the string). For example, storage for aMEDIUMTEXT value requiresL 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 particularCHAR,VARCHAR, orTEXT column value, you must take into account the character set used for that column and whether the value contains multibyte characters. In particular, when using a UTF-8 Unicode character set, you must keep in mind that not all characters use the same number of bytes.utf8mb3 andutf8mb4 character sets can require up to three and four bytes per character, respectively. For a breakdown of the storage used for different categories ofutf8mb3 orutf8mb4 characters, seeSection 12.9, “Unicode Support”.

VARCHAR,VARBINARY, and theBLOB andTEXT 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 multibyte characters

For example, aVARCHAR(255) column can hold a string with a maximum length of 255 characters. Assuming that the column uses thelatin1 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 theucs2 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).

The effective maximum number ofbytes that can be stored in aVARCHAR orVARBINARY column is subject to the maximum row size of 65,535 bytes, which is shared among all columns. For aVARCHAR column that stores multibyte characters, the effective maximum number ofcharacters is less. For example,utf8mb4 characters can require up to four bytes per character, so aVARCHAR column that uses theutf8mb4 character set can be declared to be a maximum of 16,383 characters. SeeSection 10.4.7, “Limits on Table Column Count and Row Size”.

InnoDB encodes fixed-length fields greater than or equal to 768 bytes in length as variable-length fields, which can be stored off-page. For example, aCHAR(255) column can exceed 768 bytes if the maximum byte length of the character set is greater than 3, as it is withutf8mb4.

TheNDB storage engine supports variable-width columns. This means that aVARCHAR column in an NDB Cluster table requires the same amount of storage as would any other storage engine, with the exception that such values are 4-byte aligned. Thus, the string'abcd' stored in aVARCHAR(50) column using thelatin1 character set requires 8 bytes (rather than 5 bytes for the same column value in aMyISAM table).

TEXT,BLOB, andJSON columns are implemented differently in theNDB storage engine, wherein each row in the column is made up of two separate parts. One of these is of fixed size (256 bytes forTEXT andBLOB, 4000 bytes forJSON), and is actually stored in the original table. The other consists of any data in excess of 256 bytes, which is stored in a hidden blob parts table. The size of the rows in this second table are determined by the exact type of the column, as shown in the following table:

TypeBlob Part Size
BLOB,TEXT2000
MEDIUMBLOB,MEDIUMTEXT4000
LONGBLOB,LONGTEXT13948
JSON8100

This means that the size of aTEXT column is 256 ifsize <= 256 (wheresize represents the size of the row); otherwise, the size is 256 +size + (2000 × (size − 256) % 2000).

No blob parts are stored separately byNDB forTINYBLOB orTINYTEXT column values.

You can increase the size of anNDB blob column's blob part to the maximum of 13948 usingNDB_COLUMN in a column comment when creating or altering the parent table. In NDB 8.0.30 and later, it is also possible to set the inline size for aTEXT,BLOB, orJSON column, usingNDB_TABLE in a column comment. SeeNDB_COLUMN Options, for more information.

The size of anENUM object is determined by the number of different enumeration values. One byte is used for enumerations with up to 255 possible values. Two bytes are used for enumerations having between 256 and 65,535 possible values. SeeSection 13.3.5, “The ENUM Type”.

The size of aSET object is determined by the number of different set members. If the set size isN, the object occupies(N+7)/8 bytes, rounded up to 1, 2, 3, 4, or 8 bytes. ASET can have a maximum of 64 members. SeeSection 13.3.6, “The SET Type”.

Spatial Type Storage Requirements

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

For descriptions of WKB and internal storage formats for spatial values, seeSection 13.4.3, “Supported Spatial Data Formats”.

JSON Storage Requirements

In general, the storage requirement for aJSON column is approximately the same as for aLONGBLOB orLONGTEXT 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 aJSON column such that it cannot be any larger than the value ofmax_allowed_packet.