开发者

How much real storage is used with a varchar(100) declaration in mysql?

If I have a table with a field which is declared as accepting varchar(100) and then I ac开发者_运维知识库tually insert the word "hello" how much real storage will be used on the mysql server? Also will an insert of NULL result in no storage being used even though varchar(100) is declared?

What ever the answer is, is it consistent accross different database implementations?


If I have a table with a field which is declared as accepting varchar(100) and then I actually insert the word "hello" how much real storage will be used on the mysql server?

Mysql will store 5 bytes plus one byte for the length. If the varchar is greater than 255, then it will store 2 bytes for the length.

Note that this is dependent on the charset of the column. If the charset is utf8, mysql will require up to 3 bytes per character. Some storage engines (i.e. memory) will always require the maximum byte length per character for the character set.

Also will an insert of NULL result in no storage being used even though varchar(100) is declared?

Making a column nullable means that mysql will have to set aside an extra byte per up to 8 nullable columns per row. This is called the "null mask".

What ever the answer is, is it consistent accross different database implementations?

It's not even consistent between storage engines within mysql!


It really depends on your table's charset.

In contrast to CHAR, VARCHAR values are stored as a one-byte or two-byte length prefix plus data. The length prefix indicates the number of bytes in the value. 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.

- source

UTF-8 often takes more space than an encoding made for one or a few languages. Latin letters with diacritics and characters from other alphabetic scripts typically take one byte per character in the appropriate multi-byte encoding but take two in UTF-8. East Asian scripts generally have two bytes per character in their multi-byte encodings yet take three bytes per character in UTF-8.

- source


varchar only stores what is used whereas char stores a set number of bytes.


Utf16 sometimes takes less data then utf8, for some rare languages, I don't know which ones.

Guys, is there an option to use COMPRESSed tables in MySql? Like in Apache. Thanks a lot

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜