tags:

views:

229

answers:

4

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? 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?

+9  A: 

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

cballou
Thanks alot, just what I wanted to know.
Benj
this doesn't answer the question relating to nulls.
Allain Lalonde
Yes, but the source link specifically explains the byte requirements of VARCHAR fields and gives an example using the empty string. I figured it was worth the read for him.
cballou
It certainly was cheers,
Benj
+1  A: 

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

Chris Gutierrez
+5  A: 

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!

ʞɔıu
Please mind that the numbers are for MyISAM tables. InnoDB or other engines might have other requirements. See also: http://dev.mysql.com/doc/refman/5.1/en/storage-requirements.html
johannes
A: 

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

Dan