mysql 中的 varchar(100) 声明使用了多少实际存储空间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1790514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How much real storage is used with a varchar(100) declaration in mysql?
提问by Benj
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?
如果我有一个包含声明为接受 varchar(100) 的字段的表,然后我实际上插入了单词“hello”,那么 mysql 服务器上将使用多少实际存储?即使声明了 varchar(100),插入 NULL 也会导致不使用存储吗?
What ever the answer is, is it consistent accross different database implementations?
答案是什么,它在不同的数据库实现中是否一致?
采纳答案by ???u
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?
如果我有一个包含声明为接受 varchar(100) 的字段的表,然后我实际上插入了单词“hello”,那么 mysql 服务器上将使用多少实际存储?
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.
Mysql 将存储 5 个字节加上 1 个字节的长度。如果 varchar 大于 255,那么它将存储 2 个字节的长度。
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.
请注意,这取决于列的字符集。如果字符集是 utf8,mysql 每个字符最多需要 3 个字节。某些存储引擎(即内存)将始终要求字符集的每个字符的最大字节长度。
Also will an insert of NULL result in no storage being used even though varchar(100) is declared?
即使声明了 varchar(100),插入 NULL 也会导致不使用存储吗?
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".
使列可以为空意味着 mysql 必须为每行最多 8 个可空列留出一个额外的字节。这称为“空掩码”。
What ever the answer is, is it consistent accross different database implementations?
答案是什么,它在不同的数据库实现中是否一致?
It's not even consistent between storage engines within mysql!
mysql 中的存储引擎之间甚至不一致!
回答by Corey Ballou
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.
与 CHAR 相比,VARCHAR 值存储为一字节或两字节长度的前缀加上数据。长度前缀表示值中的字节数。如果值需要不超过 255 个字节,则列使用一个长度字节,如果值可能需要超过 255 个字节,则使用两个长度字节。
- 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.
UTF-8 通常比为一种或几种语言进行的编码占用更多的空间。带有变音符号的拉丁字母和来自其他字母脚本的字符通常在适当的多字节编码中每个字符占用一个字节,但在 UTF-8 中占用两个字节。东亚文字在多字节编码中通常每个字符有两个字节,而在 UTF-8 中每个字符占三个字节。
- source
-来源
回答by Chris Gutierrez
varchar only stores what is used whereas char stores a set number of bytes.
varchar 仅存储使用的内容,而 char 存储一定数量的字节。
回答by Dan
Utf16 sometimes takes less data then utf8, for some rare languages, I don't know which ones.
utf16有时比utf8占用的数据少,对于一些罕见的语言,我不知道是哪些。
Guys, is there an option to use COMPRESSedtables in MySql? Like in Apache. Thanks a lot
伙计们,是否可以选择在 MySql 中使用压缩表?就像在 Apache 中一样。非常感谢