DECIMAL 与 INT 什么是最佳优化选择?

发布于 2024-08-13 09:03:31 字数 332 浏览 2 评论 0原文

我确信如果没有准确的上下文,这个问题是不完整的,所以我会尽力解释它,并且我会尽力说清楚
我需要存储页面上显示的全部数据,包括克、毫克、微克和千焦耳。

所有这些数据都类似于 99999.99g(千焦耳),因此我可以将 DECIMAL(7,2) 或 DECIMAL(5,2) 与较旧的 MySql 版本一起使用。

但是,我在 mysql 站点上看到,DECIMAL 数据类型比 MEDIUMINT 更重(我错了吗?),足以存储数据。
主要是,我将对小数或浮点数进行数值运算用户计算机,并且不需要再次存储它们,所以我怀疑在这种情况下最好的数据类型是什么。

那么在这种情况下最好的数据类型是什么?

I'm sure this ask is incomplete without a precise context, so I'll try to explain it, and I'll try to be clear
I need to store a whole of data rapresented on the page with grams, milligrams, micrograms and kilojoule.

All of this data is like 99999.99g (kilojoule apart), so I could use DECIMAL(7,2) or DECIMAL(5,2) with older MySql versions.

However, I've saw in mysql site, DECIMAL datatype is more heavy than a MEDIUMINT (am I wrong?) that could be enough to store the data.
Mainly, I'll do numeric operations on decimals or floats from the user machine, and don't need to store them again, so I'm doubtful about what is the best datatype in this case.

so what is the best datatype in this case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

失而复得 2024-08-20 09:03:31

数据库的目的之一是提供并确保数据完整性,并以与需要存储的数据类型的自然格式尽可能接近的格式存储数据。考虑到这一点,您不应该尝试过早地优化数据库引擎的数据类型(除非您已经完成了基准测试并且有充分的理由)。相反,您最好将精力花在使用正确的关系适当地设计表结构,并在适当的情况下创建索引,以帮助数据库引擎处理您拥有的数据量和类型。如果数据被正确规范化并在适当的地方创建索引和关系,您将会惊讶地发现数据库引擎可以处理多少数据。

因此,作为最佳实践,最好使用 DECIMAL 类型来存储小数或小数,例如价格、小数数量等。

One purpose of databases is to provide and ensure data integrity, and to store data in a format that is as close to the natural format of the type of data that needs to be stored. With that in mind, you should not attempt to optimize data types for the database engine prematurely (unless you've done benchmarks and you have a very good reason). Instead, your efforts are better spent on designing your table structures appropriately with the correct relationships and creating indexes where appropriate to help the database engine deal with the volume and types of data that you have. You will be surprised how much data a DB engine can process if the data is normalized properly and indexes and relationships are created where appropriate.

So, as a best practice, prefer the DECIMAL type for storing decimal or fractional numbers such as prices, fractional quantities etc.

寄离 2024-08-20 09:03:31

尽可能精确地按原样存储数据。当您选择它时,您始终可以将其截断。

一年后当您需要精度时,您可能会后悔没有存储额外的数据。

Store the data as it is with as much precision as you can. You can always truncate it when you select it.

You may regret not storing the extra data a year from now when you need the precision.

旧时模样 2024-08-20 09:03:31

据我了解,DECIMAL 是精确的定点算术,这是一个相对数值计算的深奥分支。当您处理具有整数部分和小数部分的数字但不允许浮点算术固有的错误时(例如会计),就会使用它。

所以我猜如果您还不知道需要使用 DECIMAL,那么您可能不知道。

As best I understand it, DECIMAL is for exact fixed point arithmetic, which is a relatively esoteric branch of numerical computing. It gets used when you work with numbers with an integer part and a fractional part but when the errors intrinsic to floating point arithmetic can't be allowed (e.g. accounting).

So I'd guess that if you don't already know that you need to use DECIMAL, you probably don't.

╄→承喏 2024-08-20 09:03:31

鉴于您需要准确性,请继续使用 DECIMAL,并且不要使用浮点类型。如果您使用 MEDIUM int,DECIMAL 将执行您必须执行的转换。

试着找个时间读一下这篇文章,看看为什么:
http://docs.sun.com/source/806-3568/ncg_goldberg。 html

Given that you need accuracy, keep using the DECIMAL, and don't use the float types. DECIMAL will do the conversions that you'd have to if you were using MEDIUM int.

Try reading this sometime for why:
http://docs.sun.com/source/806-3568/ncg_goldberg.html

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