MySQL can't handle large values even with DECIMAL - mysql

DECIMAL is supposed to be exact. It isn't. It rounds like crazy!
TABLE:
account_balance DECIMAL(18,4)
If I insert 43210987654321.9999
it rounds to 43210987654322.0000
If I insert 43210987654321.9876
it rounds to 43210987654321.9840
WHY? If I use numbers larger than 18 during table creation it is even worse.
EDIT:
I posted this clarification edit because some people do not know about the DECIMAL data type.
The DECIMAL data type is a FIXED POINT data type as opposed to a FLOATING POINT data type.
The reason it is used is:
(1) Store massive numbers that have decimal values with exact precision.
(2) Prevent rounding errors that can not be prevented when using floating point calculations. Thus any calculation on DECIMAL values should be exact... no rounding error.
POSTRESQL
http://www.postgresql.org/docs/8.1/static/datatype.html
numeric -- user-specified precision -- exact --no limit
The type numeric can store numbers with up to 1000 digits of precision and perform calculations exactly. It is especially recommended for storing monetary amounts and other quantities where exactness is required. However, arithmetic on numeric values is very slow compared to the integer types, or to the floating-point types described in the next section.
The types decimal and numeric are equivalent. Both types are part of the SQL standard.
MySQL
DECIMAL( , ) A DOUBLE stored as a string , allowing for a fixed decimal point.
http://www.htmlite.com/mysql003.php
MySQL DOC
Fixed-Point (Exact-Value) Types
The DECIMAL and NUMERIC types store exact numeric data values. These types are used when it is important to preserve exact precision, for example with monetary data. In MySQL, NUMERIC is implemented as DECIMAL, so the following remarks about DECIMAL apply equally to NUMERIC.
As of MySQL 5.0.3, DECIMAL values are stored in binary format. Previously, they were stored as strings, with one character used for each digit of the value.
http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html
The maximum value of 65 for M means that calculations on DECIMAL values are accurate up to 65 digits. This limit of 65 digits of precision also applies to exact-value numeric literals, so the maximum range of such literals differs from before. (In older versions of MySQL, decimal values could have up to 254 digits. However, calculations were done using floating-point and thus were approximate, not exact.)
Calculations involving exact-value decimal numbers are accurate to 65 digits. This is fewer than the maximum number of digits permitted before MySQL 5.0.3 (254 digits), but the exact-value precision is greater. Calculations formerly were done with double-precision floating-point, which has a precision of 52 bits (about 15 decimal digits).
http://dev.mysql.com/doc/refman/5.1/en/precision-math-decimal-changes.html

I tested on my local mysql 5.1.36 and it does not round.
What version are you using.
Also, what are you using to insert. Are you sure it is mysql rounding and not the storage before inserting to mysql.

Related

MySQL Decimal query efficiency for different number of digits

I am trying to implement a table with entry as Decimal data type using MySQL. In the link (http://dev.mysql.com/doc/refman/5.7/en/storage-requirements.html), it says that when I am using decimal to store a number that has 9 or less decimal digits, regardless of the number of digits being 7, 8 or 9, the storage space will be 4 bytes.
However, I am curious if any sql queries will take different amount of time if the decimal type number has different number of digits.
Since it take same 4 bytes, it should be same? Or would there be any difference?
The documentation says:
Values for DECIMAL (and NUMERIC) columns are represented using a
binary format that packs nine decimal (base 10) digits into four
bytes. Storage for the integer and fractional parts of each value are
determined separately. Each multiple of nine digits requires four
bytes, and the “leftover” digits require some fraction of four bytes.
It will depend on your CPU and your hardware configuration. Modern CPU's are smart enough and if you are not doing some really big calculations it should not affect the performance much. In case you are using it in a OLTP then it is not going to affect the performance very much. However in case of OLAP it might create some performance bottlenecks. But that is for something which involves really complex calculations.
Also just to add, prior to MySQL 5.0.3 decimal datatype was stored in format of string but later on with release of MySQL 5.1 and later the DECIMAL type is stored in a binary format so if we are not doing very complex calculations it should not affect the performance.

MySQL operators' precision on double

I'm trying to store a currency value in MySQL (InnoDb) and I need to write queries to aggregate some records (sum) and I'm having problem with the precision of the the output!
I've set the field's type to double and my values are some what precise but the MySQL's operators are not as precise as I need. For what it is worth, PHP's default operators are not precise enough either but there's bc* functions in PHP which can do the trick.
I was wondering if there's any way to tune the precision of MySQL operators? Including aggregation functions?
For the record, storing to and retrieving from MySQL won't affect my values which means double is an ideal type for my fields.
Since money needs an exact representation don't use data types that are only approximate like double which is a floating-point. You can use a fixed-point numeric data type for that like
numeric(15,2)
15 is the precision (total length of value including decimal places)
2 is the number of digits after decimal point
See MySQL Numeric Types:
These types are used when it is important to preserve exact precision, for example with monetary data.

mysql DECIMAL storage requirements

I was reading the mysql manual and want to make sure I understanding something correctly.
If I have DECIMAL (15,8) would this mean 6 digital before the decimal and 8 after?
If I want to move from 15,2 to allow for 8 decimals after price, then should I move to 21,8 so I don't lose any precision?
From mysql documentation : http://dev.mysql.com/doc/refman/5.7/en/fixed-point-types.html
In a DECIMAL column declaration, the precision and scale can be (and usually is) specified; for example:
salary DECIMAL(5,2)
In this example, 5 is the precision and 2 is the scale. The precision represents the number of significant digits that are stored for values, and the scale represents the number of digits that can be stored following the decimal point.
Standard SQL requires that DECIMAL(5,2) be able to store any value with five digits and two decimals, so values that can be stored in the salary column range from -999.99 to 999.99.

Are there any common gotchas with using decimal as opposed to double in MySQL?

A friend of mine said that they experienced oddities with using the decimal column to store money formats such as saving 1000.00 which would result in it being stored as 999.99.
However I just tested and 1000.00 is stored as 1000.0000 on decimal(19,4) / MySQL 5. Can anyone offer insight into why they may have experienced issues? Perhaps it was an old MySQL bug, improper calculation on the application side before saving it to the database?
This is for an ROI field and I'm storing money values which can be up in the thousands, FYI.
You should use the DECIMAL data type for exact numeric representations, and not DOUBLE. Quoting from the MySQL Documentation on Numeric Types:
MySQL supports all of the standard SQL numeric data types. These types include the exact numeric data types (INTEGER, SMALLINT, DECIMAL, and NUMERIC), as well as the approximate numeric data types (FLOAT, REAL, and DOUBLE PRECISION).
...
Because floating-point values are approximate and not stored as exact values, attempts to treat them as exact in comparisons may lead to problems. They are also subject to platform or implementation dependencies. For more information, see Section B.5.5.8, Problems with Floating-Point Values.
On 1.14.2. DECIMAL Data Type Changes where changes to the decimal type are discussed it says this:
In older versions of MySQL, decimal values could have up to 254 digits. However, calculations were done using floating-point and thus were approximate, not exact.
This could be the source of the error your friend talked about. If the value of 1000.00 was calculated it would be prone to floating point errors.
I would choose decimal for storing monetary values as the calculations will be more accurate than using double.

Reason to use DECIMAL(31,0)

What is the reason that some people from Oracle background are using DECIMAL(31,0) for integers. In MySQL it is not efficient.
Oracle implements the "INTEGER" ANSI datatype as a synonym for NUMBER(38)
For more details see "Learn Oracle: Datatypes for SQL and PL/SQL, Numbers"
However, the following table should be used as a mapping between datatype in Oracle and MySQL:
"Oracle® Database SQL Developer Supplementary Information for MySQL Migrations"
Oracle and MySQL Compared > Data Types > Comparing Data Types
The main difference, as explained here and here is that Oracle NUMBER datatype is variable-length format while MySQL DECIMAL(n) datatype used to be represented as strings that require one byte per digit or sign character (before version 5.0.3)
(Oracle NUMBER Datatype) Internal Numeric Format
Oracle Database stores numeric data in
variable-length format. Each value is
stored in scientific notation, with 1
byte used to store the exponent and up
to 20 bytes to store the mantissa. The
resulting value is limited to 38
digits of precision. Oracle Database
does not store leading and trailing
zeros. For example, the number 412 is
stored in a format similar to 4.12 x
102, with 1 byte used to store the
exponent(2) and 2 bytes used to store
the three significant digits of the
mantissa(4,1,2). Negative numbers
include the sign in their length.
Taking this into account, the column
size in bytes for a particular numeric
data value NUMBER(p), where p is the
precision of a given value, can be
calculated using the following
formula:
ROUND((length(p)+s)/2))+1
where s equals zero if the number is
positive, and s equals 1 if the number
is negative.
Zero and positive and negative
infinity (only generated on import
from Oracle Database, Version 5) are
stored using unique representations.
Zero and negative infinity each
require 1 byte; positive infinity
requires 2 bytes.
and
(MySQL) DECIMAL Data Type Changes
The storage requirements for the
integer and fractional parts of each
value are determined separately. Each
multiple of nine digits requires four
bytes, and any remaining digits left
over require some fraction of four
bytes. [...]
For example, a DECIMAL(18,9) column
has nine digits on either side of the
decimal point, so the integer part and
the fractional part each require four
bytes. A DECIMAL(20,6) column has
fourteen integer digits and six
fractional digits. The integer digits
require four bytes for nine of the
digits and three bytes for the
remaining five digits. The six
fractional digits require three bytes.