Mysql十进制数的存储方式,是好的存储十进制值作为varchar在mysql?

博客讨论了在数据库设计中将数值字段(如用户信用)存储为varchar类型的利弊。尽管varchar提供了更大的灵活性,但作者指出这会损害性能,因为varchar字段是变长的,无法预知数据大小,读取后需要转换为浮点类型,还可能导致精度问题和索引效率降低。建议尽可能使用特定的数据类型,如decimal,以提高性能和数据准确性。
摘要由CSDN通过智能技术生成

I have to store user credits which can be in decimal format like (10.99, 0.99, etc.). For now i am taking the column field as varchar(100).

what if i use decimal instead of varchar. will it be improving the performance??

解决方案

The short answer is: No, it will hurt performance.

The longer answer: VARCHAR fields are variable length, meaning, that the formatting of the database blocks cannot pre-account for the size of the data filling in there. Sure, there's a maximum length of the field, but other than that, you're off worse.

On the other hand, reading a VARCHAR and then interpreting it to fit into a floating point type will cost you. And it introduces new problem possibilities. Try to get all decimals that account for "11.99". Hope you bring the right number of decimal places, because in VARCHAR "11.99" != "11.990".

And then there's indexing. Usually, nobody indexed a decimal field, but on the off chance that you do, an index on a VARCHAR field will require more space.

All in all, it's not a good idea to save something in an unspecific field type. In the DB structure, it's usually considered best practice to be as specific as possible.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值