11.5. Data Type Storage Requirements

MySQL 5.0

11.5. Data Type Storage Requirements

The storage requirements for each of the data types supported by MySQL are listed here by category.

The maximum size of a row in a table is 65,534 bytes. Each and column accounts for only five to nine bytes toward this size.

Important: For tables using the storage engine, there is the factor of 4-byte alignment to be taken into account when calculating storage requirements. This means that all data storage is done in multiples of 4 bytes. Thus, a column value that — in a table using a storage engine other than — would take 15 bytes for storage, requires 16 bytes in an table. This requirement applies in addition to any other considerations that are discussed in this section. For example, in tables, the , , , and () column types each require 4 bytes storage per record.

In addition, when calculating storage requirements for Cluster tables, you must remember that every table using the storage engine requires a primary key; if no primary key is defined by the user, then a “hidden” primary key will be created by . This hidden primary key consumes 31-35 bytes per table record.

When calculating Cluster memory requirements, you may find useful the utility which is available on MySQLForge. This Perl script connects to a current MySQL (non-Cluster) database and creates a report on how much space that database would require if it used the storage engine.

Storage Requirements for Numeric Types

Data Type Storage Required
1 byte
2 bytes
3 bytes
, 4 bytes
8 bytes
) 4 bytes if 0 <= <= 24, 8 bytes if 25 <= <= 53
4 bytes
, 8 bytes
,), ,) Varies; see following discussion
) approximately (+7)/8 bytes

The storage requirements for (and ) are version-specific:

As of MySQL 5.0.3, values for 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
9 4

Before MySQL 5.0.3, columns are represented as strings and storage requirements are: +2 bytes if > 0, +1 bytes if = 0 (+2, if < )

Storage Requirements for Date and Time Types

Data Type Storage Required
3 bytes
8 bytes
4 bytes
3 bytes
1 byte

Storage Requirements for String Types

Data Type Storage Required
) bytes, 0 <= 255
) Prior to MySQL 5.0.3: + 1 bytes, where <= and 0 <= 255. MySQL 5.0.3 and later: + 1 bytes, where <= and 0 <= 255 or + 2 bytes, where <= and 256 <= 65535 (see note below).
) bytes, 0 <= 255
) Prior to MySQL 5.0.3: + 1 bytes, where <= and 0 <= 255. MySQL 5.0.3 and later: + 1 bytes, where <= and 0 <= 255 or + 2 bytes, where <= and 256 <= 65535 (see note below).
, +1 byte, where < 28
, +2 bytes, where < 216
, +3 bytes, where < 224
, +4 bytes, where < 232
','',...) 1 or 2 bytes, depending on the number of enumeration values (65,535 values maximum)
','',...) 1, 2, 3, 4, or 8 bytes, depending on the number of set members (64 members maximum)

For the , , and types, the values and in the preceding table should be interpreted as number of characters, and lengths for these types in column specifications indicate the number of characters. For example, to store a value requires characters plus one byte.

, , and the and 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

For example, a column can hold a string with a maximum length of 10. Assuming that the column uses the character set (one byte per character), the actual storage required is the length of the string (), plus one byte to record the length of the string. For the string , is 4 and the storage requirement is five bytes. If the same column was instead declared as , the string requires 4 + 2 = 6 bytes. Two bytes rather than one are required for the prefix because the length of the column is greater than 255 characters.

To calculate the number of bytes used to store a particular , , or column value, you must take into account the character set used for that column. In particular, when using the Unicode character set, you must keep in mind that not all characters use the same number of bytes. For a breakdown of the storage used for different categories of characters, see Section 10.7, “Unicode Support”.

Note: In MySQL 5.0.3 and later, the effective maximum length for a or column is 65,532.

As of MySQL 5.0.3, the engine supports only fixed-width columns. This means that a column from a table in a MySQL Cluster will behave as follows:

  • If the size of the column is fewer than 256 characters, the column requires one byte extra storage per row.

  • If the size of the column is 256 characters or more, the column requires two bytes extra storage per row.

Note that the number of bytes required per character varies according to the character set used. For example, if a column in a Cluster table uses the character set, then each character requires 3 bytes storage. This means that each record in such a column takes up 100 × 3 + 1 = 301 bytes for storage, regardless of the length of the string actually stored in any given record. For a column in a table using the storage engine with the character set, each record will use 1000 × 3 + 2 = 3002 bytes storage; that is, the column is 1,000 characters wide, each character requires 3 bytes storage, and each record has a 2-byte overhead because 1,000 > 256.

The and types require 1, 2, 3, or 4 bytes to record the length of the column value, depending on the maximum possible length of the type. See Section 11.4.3, “The and Types”.

and columns are implemented differently in the NDB Cluster storage engine, wherein each row in a column is made up of two separate parts. One of these is of fixed size (256 bytes), and is actually stored in the original table. The other consists of any data in excess of 256 bytes, which stored in a hidden table. The rows in this second table are always 2,000 bytes long. This means that the size of a column is 256 if <= 256 (where represents the size of the row); otherwise, the size is 256 + + (2000 – ( – 256) % 2000).

The size of an 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. See Section 11.4.4, “The Type”.

The size of a object is determined by the number of different set members. If the set size is , the object occupies +7)/8 bytes, rounded up to 1, 2, 3, 4, or 8 bytes. A can have a maximum of 64 members. See Section 11.4.5, “The Type”.