cancel
Showing results for 
Search instead for 
Did you mean: 

Replace decimal(12,4) with decimal(9,3) and decimal(13,3)

0 Kudos

Replace decimal(12,4) with decimal(9,3) and decimal(13,3)

Feature request from colinmollenhour, posted on GitHub Aug 19, 2014

MySQL decimal storage uses a minimum of 4 byes and can store 9 digits in these 4 bytes. Going up to 6 bytes you can store 13 digits.

In most cases decimal(9,3) is sufficient and it would save 2 bytes per row/column. The maximum value would be 999,999.999 (1 million) instead of 99,999,999.9999 (100 million). Unless you are selling original artwork in Iranian Rial this should be sufficient. Also according to the ISO list of currencies there are no currencies which use 4 decimal places (but plenty that use 3, so 2 is not acceptable).

For aggregation tables the better storage type would be decimal (13,3) since still 4 decimals are not needed and 13 takes the same storage space as 12. So for the same storage you can store numbers 100 times larger.

It may seem like a minor optimization but in Magento 1 my sales_ tables currently have over 500 decimal(12,4) columns (some are third-party). 2 bytes 500 columns * 1 million rows is almost 1GB of storage, not to mention the indexed columns. Not huge, but quite significant.

While you're at it, the report_event_types.event_type_id and report_event.event_type_id and report_event.subtype should be tinyint instead of smallint. Maybe these were removed or already changed in M2.. There are probably other columns that could be optimized as well. I think there is even a strong case to be made for changing store_id to "tinyint unsigned" throughout since operating over 256 stores is an edge case.

I realize the DBMS abstraction may be an impediment to this change but considering only MySQL is actually supported I think that is far less important than a database that isn't wasteful of resources.

Lastly, for anyone who is an edge case and decimal(9,3) is not sufficient it should be quite easy to install Magento, dump the database, use sed or a text editor to do a simple replacement and then re-import.

30 Comments
apiuser
New Member

Comment from FiveDigital, posted on GitHub Aug 22, 2014

I just wanted to point this topic out! We had so much pain with this issue in the time between 1.4 and 1.8 and I'm sure we weren't the only ones. So nobody wants this problems again. I'm of course not against the change and covering the rounding issues with unit tests is a must have even if the decimal format doesn't change :thumbsup:

apiuser
New Member

Comment from piotrekkaminski, posted on GitHub Aug 22, 2014

@FiveDigital agreed. It is also really hard to cover all possible cases with tests (quite a lot of possibilities incl. price rules, shipping, coupons, promos, gift cards etc) so a change in this area can introduce undetected errors.

apiuser
New Member

Comment from barbazul, posted on GitHub Aug 23, 2014

Rounding errors are far from solved. The minute you set up taxes, a notification shows up letting you know of that.

Decimal precision has already been discussed on a different thread. My thoughts on the matter are: storage space is cheap, development hours are not.

So use as many decimal places as possible to store prices and remove the multiple hard-coded rounding calculations. Precission should be easily changed for devs or maybe even for admins.

Use cases are plenty:

  • currency conversion
  • discount rules
  • financing interests
  • decimal qty items
  • very small products meant to be sold in large quantities (like nails, balloons, etc)

All of the above are present in most B2B stores I have dealt with. El 22/08/2014 12:52, "piotrekkaminski" notifications@github.com escribió:

@FiveDigital https://github.com/FiveDigital agreed. It is also really hard to cover all possible cases with tests (quite a lot of possibilities incl. price rules, shipping, coupons, promos, gift cards etc) so a change in this area can introduce undetected errors.

— Reply to this email directly or view it on GitHub https://github.com/magento/magento2/issues/636#issuecomment-53078348.

apiuser
New Member

Comment from DavidBruchmann, posted on GitHub Aug 23, 2014

Invoices from telecommunication providers often show 4 or six digits after the point to show the price for single calls / sms. Even the final value is rounded related to the currency's smallest unit you get wrong sums if only calculation with 2 decimal precision. So the assumption that digits provided by the currency are enough is just wrong and depend on individual needs.

On 8/23/14, Barbazul notifications@github.com wrote:

Rounding errors are far from solved. The minute you set up taxes, a notification shows up letting you know of that.

Decimal precision has already been discussed on a different thread. My thoughts on the matter are: storage space is cheap, development hours are not.

So use as many decimal places as possible to store prices and remove the multiple hard-coded rounding calculations. Precission should be easily changed for devs or maybe even for admins.

Use cases are plenty:

  • currency conversion
  • discount rules
  • financing interests
  • decimal qty items
  • very small products meant to be sold in large quantities (like nails, balloons, etc)

All of the above are present in most B2B stores I have dealt with. El 22/08/2014 12:52, "piotrekkaminski" notifications@github.com escribió:

@FiveDigital https://github.com/FiveDigital agreed. It is also really hard to cover all possible cases with tests (quite a lot of possibilities incl. price rules, shipping, coupons, promos, gift cards etc) so a change in this area can introduce undetected errors.

— Reply to this email directly or view it on GitHub https://github.com/magento/magento2/issues/636#issuecomment-53078348.


Reply to this email directly or view it on GitHub: https://github.com/magento/magento2/issues/636#issuecomment-53151954

apiuser
New Member

Comment from sIiiS, posted on GitHub Aug 23, 2014

As @ilol said, the IRR exchange rate in Iran for EURO is "1 IRR = 0.0000283999 EUR" and for USD is "1 IRR = 0.0000376100 USD" ... So I think even (20,10) will make sense for Iranians

apiuser
New Member

Comment from colinmollenhour, posted on GitHub Aug 25, 2014

@barbazul You seem to be equating storage with calculations.. Magento does all calculations in the PHP code which has no bearing whatsoever on storage. Only if Magento was using SQL queries with multiplications and divisions would the number of decimal places need to support more than the final result that is stored.

@DavidBruchmann Magento is not designed for telecoms and has never been and almost certainly will never be used by telecoms for usage billing. I will concede this point if a real, common case is given whereby merchants make sales in fractions smaller than their currency physically allows as per the ISO standards. Supporting cases like this when there is no good reason to at the detriment of all other merchants doesn't make sense to me, especially when the issue can be worked around easily enough by those with special needs (mysqldump | sed | mysql).

apiuser
New Member

Comment from DavidBruchmann, posted on GitHub Aug 26, 2014

@Colin Mollenhour Assumed you sell a product with the smallest fraction of a weak currency. Then you decide to sell it in a country with strong currency too. The product is there getting more expensive now because exact calculations are not possible? The telcom-example is comparable with the before mentioned nail- or balloon-problems and just showing that smallest fraction of a currency is not always related to real price-calculations.

apiuser
New Member

Comment from barbazul, posted on GitHub Aug 26, 2014

Collin: those are not separate issues. For instance, sales tables store all totals in decimal 12,4. Those totals are the result of the mentioned calculations that also happen to be stored in the DB causing all kind of rounding errors when displaying the orders and also when querying those tables for reports or when integrating with external services.

As for the requested example, check out www.maxiconsumo.com which is a B2B that sells to small supermarkets in Argentina. Prices need to use 3 decimal places though the currency (ARS) usually uses 3. El 26/08/2014 01:44, "DavidBruchmann" notifications@github.com escribió:

@Colin Mollenhour Assumed you sell a product with the smallest fraction of a weak currency. Then you decide to sell it in a country with strong currency too. The product is there getting more expensive now because exact calculations are not possible? The telcom-example is comparable with the before mentioned nail- or balloon-problems and just showing that smallest fraction of a currency is not always related to real price-calculations.

— Reply to this email directly or view it on GitHub https://github.com/magento/magento2/issues/636#issuecomment-53374911.

apiuser
New Member

Comment from acharrex, posted on GitHub Sep 08, 2014

If you configure your products with prices excluding taxes, you need to be able to define them with 4 decimals to get a price including taxes on 2 decimals.

Example: VAT: 8% Price excluding taxes: 115.8796 Price including taxes: 125.15

apiuser
New Member

Comment from colinmollenhour, posted on GitHub Sep 09, 2014

@acharrex I see your point, perhaps, but your example is flawed because 115.880 * 1.08 still rounds to 125.150. Smiley Happy