2

This Question addresses only how 'short' CHAR and VARCHAR columns are stored in an InnoDB table.

  • Does a CHAR(10) column occupy exactly 10 bytes?
  • What happens with trailing blanks?
  • What about character sets that need more than 1 byte per character?
  • How does VARCHAR(10) differ from CHAR(10)?
  • EXPLAIN implies that all indexed varchars contain a 2-byte length field. Is it really 2 bytes? Or might it be 1 byte? (cf key_len).
  • What about different ROW_FORMATs?

Not covered in this Question (to keep it from being too broad):

  • What about TEXT.
  • What about 255, 191, off-page storage, etc.
  • What happens in an index starting with a char/varchar. (Think: removal of common prefix.)
  • What happens with char/varchar when involved in a MEMORY temp table. Also, what changes happen in version 8.0.
  • ROW_FORMAT has a significant impact on longer string columns, primarily in deciding when off-page storage is used.
1

1 Answer 1

2

From MySQL Documentation:

The difference between CHAR and VARCHAR values is the way they are stored, CHAR (10) requires 10 bytes of storage no matter how many characters you use because the data is right-padded with spaces, VARCHAR (10) only takes 1 byte (in 1 byte character set) + length prefix (1 when the length is 255 or less, 2 otherwise... I don't know why key_len for EXPLAIN add 2 bytes)

I don't understand what you mean with trailing blanks, although I can imagine you are referring to the excess of trailing spaces, with VARCHAR these are truncated with a warning, meanwhile in CHAR columns these spaces are truncated silently, this has some sense cause CHAR are stored with trailing blanks at the end.

Regarding character set in this link you can see that the number of characters for the CHAR or VARCHAR is the same, although, your storage will require from 1 to 4 bytes per character, here is the list of supported character set and here the bytes per character.

What I've read of different rows format for InnoDB

Redundant Row Format Characteristics:

Internally, InnoDB stores fixed-length character columns such as CHAR(10) in a fixed-length format. InnoDB does not truncate trailing spaces from VARCHAR columns.
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.

COMPACT Row Format Characteristics:

  • Internally, for nonvariable-length character sets, InnoDB stores fixed-length character columns such as CHAR(10) in a fixed-length format.

    InnoDB does not truncate trailing spaces from VARCHAR columns.

  • Internally, for variable-length character sets such as utf8mb3 and utf8mb4, InnoDB attempts to store CHAR(N) in N bytes by trimming trailing spaces. If the byte length of a CHAR(N) column value exceeds N bytes, InnoDB trims trailing spaces to a minimum of the column value byte length. The maximum length of a CHAR(N) column is the maximum character byte length × N.

    InnoDB reserves a minimum of N bytes for CHAR(N). Reserving the minimum space N in many cases enables column updates to be done in place without causing fragmentation of the index page. By comparison, for ROW_FORMAT=REDUNDANT, CHAR(N) columns occupy the maximum character byte length × N.

    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.

    ROW_FORMAT=DYNAMIC and ROW_FORMAT=COMPRESSED handle CHAR storage in the same way as ROW_FORMAT=COMPACT.

...

DYNAMIC and COMPRESSED row formats are variations of the COMPACT row format and therefore handle CHAR storage in the same way as the COMPACT row format

4
  • The link to "About using UTF-8 fields in MySQL" is mostly still correct (8 years later), but needs updating to reflect utf8mb4 and ROW_FORMATs. And MySQL 8.0 is moving away from fixed-length for temp storage.
    – Rick James
    Jan 24, 2018 at 21:53
  • 1
    As for the "2" that EXPLAIN says (when it should be "1"), that's EXPLAIN being too lazy to get the exact length for the hidden length field.
    – Rick James
    Feb 13, 2018 at 2:24
  • Thanks a lot, that explain why is not considering the lenght for the lengh prefix bytes. Feb 13, 2018 at 8:43
  • The final link in this answer is broken, possibly due to a change to the docs since this answer was written. There are, however, some other sources for the same prose findable on Google (e.g. docs.oracle.com/cd/E17952_01/mysql-5.1-en/…, plus third parties, some honest and some shady, that have either archived or plagiarised it). Based on those sources, I've done my best to figure out what's a quote and what's not, and mark up each distinct quote with its own quote block. However, this answer could still do with the link fixing and the formatting improved.
    – Mark Amery
    Nov 3, 2019 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.