Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decimal VS Int in MySQL?

Are there any performance difference between decimal(10,0) unsigned type and int(10) unsigned type?

like image 876
TheOnly92 Avatar asked Apr 21 '10 10:04

TheOnly92


People also ask

What is the difference between INT and decimal in MySQL?

The differences is that the decimal is packed and some operations on such data type might be slower then on normal INT types which map directly to machine represented numbers.

What is a decimal in MySQL?

In Transact-SQL statements, a constant with a decimal point is automatically converted into a numeric data value, using the minimum precision and scale necessary. For example, the constant 12.345 is converted into a numeric value with a precision of 5 and a scale of 3.

Can INT have decimals MySQL?

MySQL assigns the storage for integer and fractional parts separately. MySQL uses binary format to store the DECIMAL values. It packs 9 digits into 4 bytes. For example, DECIMAL(19,9) has 9 digits for the fractional part and 19-9 = 10 digits for integer part.

What is an INT in MySQL?

In MySQL, INTEGER (INT) is a numeric value without a decimal. It defines whole numbers that can be stored in a field or column. In addition, MySQL supports the display_width attribute (for example, INT(1)) and the ZEROFILL attribute, which automatically adds zeros to the value depending on the display width.


2 Answers

It may depend on the version of MySQL you are using. See here.

Prior to MySQL 5.0.3, the DECIMAL type was stored as a string and would typically be slower. However, since MySQL 5.0.3 the DECIMAL type is stored in a binary format so with the size of your DECIMAL above, there may not be much difference in performance.

The main performance issue would have been the amount of space taken up by the different types (with DECIMAL being slower). With MySQL 5.0.3+ this appears to be less of an issue, however if you will be performing numeric calculations on the values as part of the query, there may be some performance difference. This may be worth testing as there is no indication in the documentation that i can see.

Edit: With regards to the int(10) unsigned, i took this at face value as just being a 4 byte int. However this has a maximum value of 4294967295 which strictly doesn't provide the same range of numbers as a DECIMAL(10,0) unsigned .

As @Unreason pointed out, you would need to use a bigint to cover the full range of 10 digit numbers, pushing the size up to 8 bytes.

A common mistake is that when specifying numeric columns types in MySQL, people often think the number in the brackets has an impact on the size of the number they can store. It doesn't. The number range is purely based on the column type and whether it is signed or unsigned. The number in the brackets is for display purposes in results and has no impact on the values stored in the column. It will also have no impact of the display of the results unless you specify the ZEROFILL option on the column as well.

like image 162
Jarod Elliott Avatar answered Sep 22 '22 13:09

Jarod Elliott


According to the mysql data storage your decimal will require

DECIMAL(10,0): 4 bytes for 9 digits and 1 byte for the remaining 10th digit, so in total five bytes (assuming my reading of documentation is correct).

INT(10): will need BIGINT which is 8 bytes.

The differences is that the decimal is packed and some operations on such data type might be slower then on normal INT types which map directly to machine represented numbers.

Still I would do your own tests to confirm the above reasoning.

EDIT: I noticed that I did not elaborate on the obvious point - assuming the above logic is sound the difference in size required is 60% more space needed for BIGINT variant.

However this does not directly translate to penalties due to the fact that data is normally not written byte by byte. In case of selects/updates of many rows you should see the performance loss/gain, but in case of selecting/updating a small number of rows the filesystem will fetch blocks from the disk(s) which will normally get/write multiple columns anyway.
The size (and speed) of indexes might be more directly impacted. However, the question on how the packing influences various operations still remains open.

like image 30
Unreason Avatar answered Sep 20 '22 13:09

Unreason