Decimal(2,1) 的存储要求是什么?
MySQL 5.6手册中说:
DECIMAL(和 NUMERIC)列的值使用二进制格式表示,该格式将九个十进制(以 10 为基数)数字打包为四个字节。每个值的整数部分和小数部分的存储是单独确定的。每个九位数的倍数需要四个字节,“剩余”数字需要四个字节的一部分。多余数字所需的存储空间如下表所示:
+----------------------------------+
|Leftover Digits | Number of Bytes |
+----------------------------------+
| 0 | 0 |
+----------------------------------+
| 1 | 1 |
+----------------------------------+
| 2 | 1 |
+----------------------------------+
| 3 | 2 |
+----------------------------------+
| 4 | 2 |
+----------------------------------+
| 5 | 3 |
+----------------------------------+
| 6 | 3 |
+----------------------------------+
| 7 | 4 |
+----------------------------------+
| 8 | 4 |
+----------------------------------+
我的问题是,如果小数点两边的数字都少于九位,是否仍会用完 4 个字节,还是会被视为“剩余< /强>”?
It says in MySQL 5.6 manual that:
Values for DECIMAL (and NUMERIC) columns are represented using a binary format that packs nine decimal (base 10) digits into four bytes. Storage for the integer and fractional parts of each value are determined separately. Each multiple of nine digits requires four bytes, and the “leftover” digits require some fraction of four bytes. The storage required for excess digits is given by the following table:
+----------------------------------+
|Leftover Digits | Number of Bytes |
+----------------------------------+
| 0 | 0 |
+----------------------------------+
| 1 | 1 |
+----------------------------------+
| 2 | 1 |
+----------------------------------+
| 3 | 2 |
+----------------------------------+
| 4 | 2 |
+----------------------------------+
| 5 | 3 |
+----------------------------------+
| 6 | 3 |
+----------------------------------+
| 7 | 4 |
+----------------------------------+
| 8 | 4 |
+----------------------------------+
My question is, if the digits are less than nine on each side of the decimal, would it still use up 4 bytes or will they be considered as "leftovers"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我使用 MyISAM 表进行了一些测量,通过在向六行数据添加一些列后比较表 dec.MYD 的文件大小来获得答案。这是我得到的结果:
结论:
请注意,测试并不是那么简单,因为第一列似乎始终是 7 个字节,无论它是否为
DECIMAL(2,1)或<代码>INT。您需要添加更多列才能重现上述结果。
I did some measurements using
MyISAM
table to get the answer by comparing the file size of the tabledec.MYD
after adding some columns to six rows of data. Here is what I get:The Conclusion:
Note that testing is not so straight forward as the first column seems to be always 7 bytes regardless of whether it is
DECIMAL(2,1)
orINT
. You need to add more columns to reproduce the above result.