Search code examples
mysqlvarchar

MySQL varchar size storage - one byte, two byte conversion


According to the MySQL documentation, VARCHAR will use 1 byte to denote the length of the string provided all values in that column are of length 0-255, and otherwise 2 bytes will be used.

What happens if you have large amounts of data in a column which are all 255 or less, and after entries, a single entry of length 256 is entered. Does a re-format operation kick in to update the other strings in this column to use 2 bytes instead of 1? Is there a way to preempt this operation aside from inserting invalid/ignored data? Something like myField VARCHAR(1000) not null always-use-two-bytes, (obviously it would be cleaner than that... but you get the idea).


Solution

  • The actual quote from the docs is:

    A column uses one length byte if values require no more than 255 bytes, two length bytes if values may require more than 255 bytes.

    What this means to me is that if you have varchar(256+) then it's going to use 2 bytes from the beginning.