content_image/745129.jpg
<img src='content_image/745129.jpg'>

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 multibyte characters. In particular, when using a utf8  Unicode character set, you must keep in mind that not all characters use the same number of bytes. utf8mb3  and utf8mb4  character sets can require up to three and four bytes per character, respectively. For a breakdown of the storage used for different categories of utf8mb3  or utf8mb4  characters, see Section 10.9, “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 multibyte 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). 

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 multibyte characters, the effective maximum number of characters  is less. For example, utf8mb4 characters can require up to four bytes per character, so a VARCHAR  column that uses the utf8mb4 character set can be declared to be a maximum of 16,383 characters. See Section 8.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, a CHAR(255)  column can exceed 768 bytes if the maximum byte length of the character set is greater than 3, as it is with utf8mb4 . 

