Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to store a file size in bytes?

What's the best way to store a file size in bytes in database?

Considering that the size can be huge MB, GB, TB...

I'm using bigint (max: 9.223.372.036.854.775.807), but is it the best way?

like image 674
Zanoni Avatar asked Jun 16 '09 02:06

Zanoni


2 Answers

A 64-bit integer is all you need.

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.

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.

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).

like image 107
Arafangion Avatar answered Sep 20 '22 22:09

Arafangion


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.

like image 32
Robert Harvey Avatar answered Sep 22 '22 22:09

Robert Harvey