Do char / varchar / text / longtext columns in MyS

2019-03-05 23:47发布

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 following columns and i have put in a given string in it.

CHAR (72) - can store upto 72 characters - i put in a string "abcd"
VARCHAR (72) - can store upto 72 characters - i put in a string "abcd"
TINYTEXT - can store upto 255 characters - i put in a string "abcd"
TEXT - can store upto 65,535 characters - i put in a string "abcd"
LONGTEXT - can store upto 4,294,967,295 characters - i put in a string "abcd"

How much actual physical space will be occupied by each of these columns for a row?

The full size upto which each column is capable of storing?

Or only as much as required for storing "abcd"?

1条回答
一纸荒年 Trace。
2楼-- · 2019-03-06 00:32

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.

查看更多
登录 后发表回答