2

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.

4
  • Any reason DECIMAL(3,6) doesn't fit the bill?
    – Jon
    Oct 23, 2011 at 21:58
  • Also, 9 significant digits is the range 0 to 10^10 - 1 while a 4-byte unsigned int covers 0 to 2^32 - 1 < 10^10 - 1. You need at least 34 bits to store these numbers.
    – Jon
    Oct 23, 2011 at 22:05
  • 4
    @Jon: DECIMAL(9,6) you would say
    – zerkms
    Oct 23, 2011 at 22:10
  • Yes...because decimal uses 8 bytes which means by table will be almost double in size.
    – DD.
    Oct 24, 2011 at 22:41

2 Answers 2

2

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.

-1

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.

3
  • 3
    You can do fixed-point math with plain ints as well. You just lose range in exchange for simulating floats with ints, plus gain all the headaches of having to do the various conversions and range checks. Not saying this is preferable, just that it's POSSIBLE.
    – Marc B
    Oct 23, 2011 at 22:13
  • @Marc - Thanks a lot, for letting me know about this. +1 :) Oct 23, 2011 at 22:43
  • My whole point is that I dont want to use decimal as it uses 8 bytes...the data can easily fit into an int if I convert as Marc B suggests. I was kind of hoping there was some clever alternative but doesnt look like it. Either I can use decimal and need double the storage or I can use int but then have to do all the conversions.
    – DD.
    Oct 24, 2011 at 22:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.