Why is there a performance difference?
This is due to the use of integer division.
> select 643/109;
5
> select round(643/109, 0);
5.0
> select 643/109.0;
5.89908256880734
> select round(643/109.0, 0);
6.0
The first command shows that integer division is occurring. So, change the divisor or dividend to float and you get the expected result as shown in the last command.
source to share
In your first query, the select ROUND(5.8990,0)
output is 6.000 since the value, 5.8990, is considered decimal due to the presence of a dot. But in the case of the second query, select ROUND(643/109,0)
643 and 109 are considered to be integers, so the return value is 5 and not 5.8990 as you expected. In order to change the return to 6.000, all you have to do is pass a second query like this:
select ROUND(643.00/109.00,0)
source to share
In the second case, you are dividing two integers, so you end up with an integer. The split result is 5.89, but it will just discard all the leftovers because it needs an integer as its return, so you get 5. The result is truncated. Additional Information
If you want to have a decimal result, just use the decimal number in any part of the division, for example
select ROUND(643.0/109,0) -- returns 6
-- or
select ROUND(643/109.0,0) -- also returns 6
In the first case, ROUND does not truncate the result, it is rounded up to the next nearest integer (in accordance with the parameters that you specified). You can get the same result if you use
select ROUND(5.8990, 0, 1)
-- returns 5
More information about ROUND here
source to share