Decimal(2,1) 的存储要求是什么?

发布于 2024-12-21 13:56:03 字数 1188 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恋竹姑娘 2024-12-28 13:56:04

我使用 MyISAM 表进行了一些测量,通过在向六行数据添加一些列后比较表 dec.MYD 的文件大小来获得答案。这是我得到的结果:

Original:
-rw-rw----. 1 mysql mysql   54 Dec 17 18:49 dec.MYD

Add one column of DECIMAL(2,1):
-rw-rw----. 1 mysql mysql   66 Dec 17 18:51 dec.MYD

Further add one column of DECIMAL(4,1):
-rw-rw----. 1 mysql mysql   84 Dec 17 18:51 dec.MYD

结论:

DECIMAL(2,1) requires (66-54)/6 = 2 bytes in total
DECIMAL(4,1) requires (84-66)/6 = 3 bytes in total

请注意,测试并不是那么简单,因为第一列似乎始终是 7 个字节,无论它是否为 DECIMAL(2,1)或<代码>INT。您需要添加更多列才能重现上述结果。

I did some measurements using MyISAM table to get the answer by comparing the file size of the table dec.MYD after adding some columns to six rows of data. Here is what I get:

Original:
-rw-rw----. 1 mysql mysql   54 Dec 17 18:49 dec.MYD

Add one column of DECIMAL(2,1):
-rw-rw----. 1 mysql mysql   66 Dec 17 18:51 dec.MYD

Further add one column of DECIMAL(4,1):
-rw-rw----. 1 mysql mysql   84 Dec 17 18:51 dec.MYD

The Conclusion:

DECIMAL(2,1) requires (66-54)/6 = 2 bytes in total
DECIMAL(4,1) requires (84-66)/6 = 3 bytes in total

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) or INT. You need to add more columns to reproduce the above result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文