Storing small decimals MySQL - mysql

I need to store a large volume of small decimals numbers (3 digits before the decimal, 6 digits after the decimal).
From my understanding of the spec, this will require 8 bytes. I could store the number as an int which requires only 4 bytes and convert after retrieval using a fixed scale factor.
Is there a better alternative instead of using an int, I can't easily do any arithmetic on the numbers?
Thanks.

I do not think this is correct.
DECIMAL(9,6) should do the job.
It will require 2 bytes for the 3 digits and 3 bytes for the 6 digits according to mysql 5.1 manual. IMHO that´s 5 bytes not 8 bytes in total.
You will therefore not require a lot more memory than with the integer "hacking" you proposed. I would definitely go with decimal in your case.

No, it won't work if you are using "int" data type of MySQL. This is because integers can't handle decimal precision.
According to your question, you should be using "Fixed Point Data Types", which will benefit you in large calculations & monetary data. In MySQL, the required data type is "DECIMAL" and you can read more on it here.
The proper syntax in you case will be "DECIMAL (9, 6)", where 9 means that values can be stored with up to 9 digits in total, of which 6 digits are after the decimal point and 3 digits are before the decimal point.
Hope it helps.

Related

How many bytes does an unsigned DECIMAL(3,2) use in MySQL

I was wondering how many bytes an unsigned DECIMAL(3,2) uses in SQL. The documentation that I find online is not very clear, especially not when you have an unsigned DECIMAL.
The documentation seems pretty clear:
Values for DECIMAL columns are stored using a binary format that packs
nine decimal digits into 4 bytes. The storage requirements for the
integer and fractional parts of each value are determined separately.
Each multiple of nine digits requires 4 bytes, and any remaining
digits left over require some fraction of 4 bytes. The storage
required for remaining digits is given by the following table.
So, the integer part of DECIMAL(3, 2) has 1 digit and the fractional part has 2 digits. According to the table, 1-2 digits require one byte. Hence the total is two bytes.
The simple answer, which is usually correct or very close:
The storage requirement for DECIMAL(m,n) is m/2.
In your case 3/2 = 1.5 is very close. In this case, round up to get the correct "2".

Crypto Currency MySQL Datatypes ?

The infamous question about datatypes when storing money values in an SQL database.
However in these trying times, we now have currencies that have worth up to 18 decimal places (thank you ETH).
This now reraises the classic argument.
IDEAS
Option 1 BIGINT Use a big integer to save the real value, then store how many decimal places the currency has (simply dividing A by 10^B in translation)?
Option 2 Decimal(60,30) Store the datatype in a large decimal, which inevitibly will cost a large amount of space.
Option 3 VARCHAR(64) Store in a string. Which would have a performance impact.
I want to know peoples thoughts and what they are using if they are dealing with cryptocurrency values. As I am stumped with the best method for proceeding.
There's a clear best option out of the three you suggested (plus one from the comments).
BIGINT — uses just 8 bytes, but the largest BIGINT only has 19 decimal digits; if you divide by 1018, the largest value you can represent is 9.22, which isn't enough range.
DOUBLE — only has 15–17 decimal digits of precision; has all the known drawbacks of floating-point arithmetic.
VARCHAR — will use 20+ bytes if you're dealing with 18 decimal places; will require constant string↔int conversions; can't be sorted; can't be compared; can't be added in DB; many downsides.
DECIMAL(27,18) – if using MySQL, this will take 12 bytes (4 for each group of 9 digits). This is quite a reasonable storage size, and has enough range to support amounts as large as one billion or as small as one Wei. It can be sorted, compared, added, subtracted, etc. in the database without loss of precision.
I would use DECIMAL(27,18) (or DECIMAL(36,18) if you need to store truly huge values) to store cryptocurrency money values.

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 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.

Mysql DECIMAL storage

Hi I am creating a very big table using DECIMAL data types. its gonna be 50 million rows to start and grow from there, so I am concerned with storage. I need DECIMAL as I need exact representation, and the documentation is clear that if you want exact representation you must use DECIMAL.
The mysql manual is quite clear on DECIMAL storage reqs, stating :
As of MySQL 5.0.3, values for DECIMAL 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. The storage required for excess digits is given by the following table.
Leftover Digits Number of Bytes
0 0
1 1
2 1
3 2
4 2
http://dev.mysql.com/doc/refman/5.0/en/storage-requirements.html
So that implies that a DECIMAL(12,4) would require:
8 bytes for integer portion and 2 types for the 'leftover' portion for total 10 bytes.
So 1st question is, wouldn't DECIMAL(18,4) use the same 10 bytes of storage? If I want to save storage, I would need to bump down to DECIMAL(9,4) and that's not an option for me.
IF so, 2nd question any idea if mysql processes DECIMAL(12,4) more efficiently (internally) than DECIMAL(18,4)? I dont think that question is necessarily answerable, but thought I would give it a shot! maybe someone has done some sort of benchmark...
thx.
Don
You have your calculations wrong.
If I understand correctly what the page you link to describes, a DECIMAL(12,4) would require:
The integer portion is 8 digits, so 4 bytes
The fractional part is 4 digits, so 2 bytes.
Total is 6 bytes.
For DECIMAL(18,4), which has 14 integer digits and 4 fractional digits, it would require (4+3)+(2) = 9 bytes.
Without thinking too much about it, I believe ybercube has the correct answer. Having said that, couldn't you just go ahead and easily test this yourself by creating a table and doing some inserts? "show table status" should probably have the information you need.