What sql data type should I use to store execution time in milliseconds?
I need to store the 开发者_如何学Goexecution time (in milliseconds) of a function in a sql field. What data type should I use?
I think the int
would be the best type for you.
I would use a standard 32-bit integer (or whatever comes close in your RDBMS). If your RDBMS has a specific type for timespans, use that, but all RDBMS that I know of only have timestamp-like data types.
Added: A 32-bit integer is enough for a bit over 49 days. If you need longer than that, you'll have to use a bigger data type, but then I doubt you'll need a millisecond precision. At a second precision it is good for about 136 years. If you need more than that, you've got bigger problems.
You might consider storing your elapsed time in 2 columns (a 32-bit int
for the seconds, and a 16-bit smallint
for the milliseconds). That will cover a range of some 68 years and take 6 bytes per row.
Alternatively, storing your elapsed time in the money
data type, as seconds, is convenient. It stores 4 decimal places of precision and has a range of c. -2^3 to +2^63 - 1, or more precisely, -922,337,203,685,477.5808) through +922,337,203,685,477.5807. Discounting negative values, +922,337,203,685,477.5807 seconds covers some 29m years. Using money
will take 8 bytes per row.
Or the decimal(p,q)
datatype. Choose your overall precision p
and number of decimal places q
. Storage required is is 5, 9, 13, or 17 bytes depending on the value of p
.
精彩评论