What type to use in code and database for huge file size?
My project (C#
) deals with many files about 1 MB ~ 2 GB
in size. SQL server 2008
database is used. In long-term I need to do some operations over them like to开发者_Python百科tal sum of their size and ...
At first glance I was planned to store their size in byte (in C# code long
, in database BigInt
) because of file size nature and its precise. I was thinking maybe its a better idea to use a double and consider file size in MB because most of them are 1~2000 MB
and make more sense while talking about files of this project. Is there any advantage/disadvantage over these two kind of designs both in code (performance issues when there are many mathematics operations) and database (batch operations over many files) ?
You should use long
/BigInt, for a few reasons:
- File sizes are in bytes, which are a precise discrete measure -- so you might as well use a precise discrete value.
- If you use a decimal, it's hard to know what scale you're dealing with -- kb? bytes? mb? But if you're dealing with integers, you'll probably know that it's in bytes.
- Not a super big deal, but performance is slightly better on most processors with longs than with decimals.
- As far as I know, it's the conventional thing to do.
Store the exact value in a long
, formatting/interpreting as MB, etc should be done by the client.
Either use uint
or ulong
, since:
- you can't have negative file sizes, and
- you can't have files that take fractions of bytes (e.g. 3,231.5 bytes is not a valid answer)
With a max value of 4,294,967,295 a uint
seems the best solution for files of the size you're describing, as long as you can guarantee they'll never be any larger than this many bytes. It takes less space than a long
or a double
, and with only 32 bits it might even be quicker to evaluate depending on the processor and such.
Why not Decimal? Decimal.MaxValue
has this property:
The value of this constant is positive 79,228,162,514,264,337,593,543,950,335
SQL supports this for most of the .Net valid range. Limitations in conversion are noted here.
If i remember correctly, file size is already returned as a long in .Net. Also, floats are 64bit and longs are 64bit, but floats are slower.
Also, if you do calculations are doubles, you will have to convert a long to a double on every file, while if you wait until the end, then there's only one conversion.
Do all of your calculations as bytes, then at the last step you can convert to a float to report something like 2.051GB or whatever.
精彩评论