What's the best way to store a file size in bytes in database?
在数据库中以字节为单位存储文件大小的最佳方法是什么?
Considering that the size can be huge MB, GB, TB...
考虑到大小可以是巨大的MB,GB,TB ......
I'm using bigint (max: 9.223.372.036.854.775.807), but is it the best way?
我正在使用bigint(最大值:9.223.372.036.854.775.807),但这是最好的方法吗?
2 个解决方案
#1
That's the type I would choose. It corresponds to the long type in c# (a 64 bit number), and it is the same type that is used by Windows to store file sizes.
这是我会选择的类型。它对应于c#中的long类型(64位数),它与Windows用于存储文件大小的类型相同。
#2
A 64-bit integer is all you need.
您只需要一个64位整数。
If bigint has a maximum value of 9.223.372.036.854.775.807, then that suggests a signed 64-bit integer, which is perfectly adequate.
如果bigint的最大值为9.223.372.036.854.775.807,则建议使用带符号的64位整数,这是完全足够的。
From the description, it does not look like 32-bit integers will do what you need, so unless you actually need to support larger sizes than 9.223.372.036.854.775.807, then bigint is the most efficient form you could possibly choose.
从描述中看,32位整数看起来不像你需要的那样,所以除非你真的需要支持比9.223.372.036.854.775.807更大的大小,否则bigint是你可能选择的最有效的形式。
If you needed larger values (I can't imagine why), then you'd need to either store it as a string, or find a large-number library that will use as many bytes as neccessary to store the number (ie, has no maximum size).
如果你需要更大的值(我无法想象为什么),那么你需要将它存储为字符串,或者找到一个大号的库,它将使用尽可能多的字节来存储数字(即,没有最大尺寸)。
#1
That's the type I would choose. It corresponds to the long type in c# (a 64 bit number), and it is the same type that is used by Windows to store file sizes.
这是我会选择的类型。它对应于c#中的long类型(64位数),它与Windows用于存储文件大小的类型相同。
#2
A 64-bit integer is all you need.
您只需要一个64位整数。
If bigint has a maximum value of 9.223.372.036.854.775.807, then that suggests a signed 64-bit integer, which is perfectly adequate.
如果bigint的最大值为9.223.372.036.854.775.807,则建议使用带符号的64位整数,这是完全足够的。
From the description, it does not look like 32-bit integers will do what you need, so unless you actually need to support larger sizes than 9.223.372.036.854.775.807, then bigint is the most efficient form you could possibly choose.
从描述中看,32位整数看起来不像你需要的那样,所以除非你真的需要支持比9.223.372.036.854.775.807更大的大小,否则bigint是你可能选择的最有效的形式。
If you needed larger values (I can't imagine why), then you'd need to either store it as a string, or find a large-number library that will use as many bytes as neccessary to store the number (ie, has no maximum size).
如果你需要更大的值(我无法想象为什么),那么你需要将它存储为字符串,或者找到一个大号的库,它将使用尽可能多的字节来存储数字(即,没有最大尺寸)。