views:

129

answers:

2

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?

+6  A: 

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.

Robert Harvey
Oh and because Microsoft uses it that must make it the right way to do it! JK! ;-)
Lucas McCoy
Did I detect a snark?
Robert Harvey
@Lucas, not because MS uses it, just because it's a reasonably big number (MS is an effect here, not the cause). Do *you* have a single file clocking in at 10 exabytes? Let's see, I have a newfangled terabyte external drive (cost me ~$100). To store a file that big would take one million of those drives - my house *and* my bank balance is too small for that. [I did see your smiley :-)]
paxdiablo
You should use whatever your operating system uses for handling file sizes...
vanja.
+2  A: 

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

Arafangion