9

Anybody knows how MySQL allocates disk space for fields like "TEXT" or "BLOB"

For example, what happens when I insert 10kb string into "TEXT" column? Is the entire 65kb data allocated or only 10kb?

1
  • Don't confuse limits with the space allocated. VARCHAR(255) holds a maximum of 255 characters. There are some types which do reserve space, like CHAR, but these are the exception.
    – tadman
    Aug 16, 2016 at 7:45

1 Answer 1

12

This is explained in the documentation: http://dev.mysql.com/doc/refman/5.7/en/storage-requirements.html

BLOB, TEXT              L + 2 bytes, where L < 2^16
MEDIUMBLOB, MEDIUMTEXT  L + 3 bytes, where L < 2^24 
LONGBLOB, LONGTEXT      L + 4 bytes, where L < 2^32

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.

So in short, the whole 65kb is not wasted.

Your Answer

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

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