Do varchar/text/longtext columns in MySQL occupy full length size in filesystem even for incomplete cells?
Referring to this blog article for example I have the followin
Check out http://dev.mysql.com/doc/refman/5.7/en/string-type-overview.html
Basically, all of these types except for the CHAR
are variable length.
For instance, if you have VARCHAR(72)
and you write abcd
into it, you will store 5 bytes. 4 bytes for each character, and a 1 byte prefix to store the length of the string (which is 4).
If the length of the string is over 255 characters, then the VARCHAR prefix will be 2 bytes. So a VARCHAR(300)
with a 256 character string stored in it will take up 258 bytes.
TINYTEXT
has a 1 byte prefix always, because you can only store 255 characters in it, so abcd
would take 5 bytes.
TEXT
has a 2 byte prefix, so abcd
would be 6 bytes.
LONGTEXT
has a 4 byte prefix, so abcd
would be 8 bytes.
Lastly, there's the nearly useless CHAR
type. A CHAR(72)
will always take up 72 bytes no matter what you store in it. It's really only useful for super short fields where there is always the exact same number of characters in the field. Like Y
or N
would be a good CHAR(1)
candidate.