Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a speed difference in ordering by int vs. float?

When retrieving entries in a database, is there a difference between storing values as a float or decimal vs. an int when using ORDERBY in a SELECT statement?

like image 638
Matt Norris Avatar asked Sep 09 '11 11:09

Matt Norris


People also ask

Is floating point faster than integer?

In computer systems, integer arithmetic is exact, but the possible range of values is limited. Integer arithmetic is generally faster than floating-point arithmetic. Floating-point numbers represent what were called in school “real” numbers (i.e., those that have a fractional part, such as 3.1415927).

Are float operations slower than int?

Floating-point operations are always slower than integer ops at same data size. Smaller is faster. 64 bits integer precision is really slow. Float 32 bits is faster than 64 bits on sums, but not really on products and divisions.

Is float more accurate than int?

A floating point number is more precise, as it represents a number upto 6 decimal digits along with integer part, where as an integer represents only the integer part of the given number.

Are floats faster?

Floats are very generally faster than doubles and take less memory (FPU features vary).


3 Answers

It depends. You didn't specify the RDBMS so I can only speak to SQL Server specifically but data types have different storage costs associated with them. Ints range from 1 to 8 bytes, Decimals are 5-17 and floats are 4 to 8 bytes.

The RDBMS will need to read data pages off disk to find your data (worst case) and they can only fit so many rows on an 8k page of data. So, if you have 17 byte decimals, you're going to get 1/17th the amount of rows read off disk per read than you could have if you sized your data correctly and used a tinyint with a 1 byte cost to store X.

That storage cost will have a cascading effect when you go to sort (order by) your data. It will attempt to sort in memory but if you have a bazillion rows and are starved for memory it may dump to temp storage for the sort and you're paying that cost over and over.

Indexes may help as the data can be stored in a sorted manner but again, if getting that data into memory may not be as efficient for obese data types.

[edit]

@Bohemian makes a fine point about the CPU efficiency of integer vs floating point comparisons but it is amazingly rare for the CPU to be spiked on a database server. You are far more likely to be constrained by the disk IO subsystem and memory which is why my answer focuses on the speed difference between getting that data into the engine for it to perform the sort operation vs the CPU cost of comparison.

like image 105
billinkc Avatar answered Jun 20 '23 21:06

billinkc


(Edited) Since both int and float occupy exactly the same space on disk, and of course in memory - ie 32 bits - the only differences are in the way they are processed.

int should be faster to sort than float, because the comparison is simpler: Processors can compare ints in one machine cycle, but a float's bits have to be "interpreted" to get a value before comparing (not sure how many cycles, but probably more than one, although some CPUs may have special support for float comparison).

like image 28
Bohemian Avatar answered Jun 20 '23 19:06

Bohemian


In general, the choice of datatypes should be driven by whether the datatype is appropriate for storing the values that are required to be stored. If a given datatype is inadequate, it doesn't matter how efficient it is.

In terms of disk i/o the speed difference is second order. Don't worry about second order effects until your design is good with regard to first order effects.

Correct index design will result in a huge decrease in delays when a query can be retrieved in sorted order to begin with. However, speeding up that query is done at the cost of slowing down other processes, like processes that modify the indexed data. The trade off has to be considered to see whether it's worth it.

In short, worry about the stuff that's going to double your disk i/o or worse before you worry about the stuff that's going to add 10% to your disk i/o

like image 34
Walter Mitty Avatar answered Jun 20 '23 21:06

Walter Mitty