Decimal VS Int in MySQL?

前端 未结 4 685
名媛妹妹
名媛妹妹 2020-12-31 14:00

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

相关标签:
4条回答
  • 2020-12-31 14:23

    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.

    0 讨论(0)
  • 2020-12-31 14:25

    I doubt such a difference can be performance related at all.
    Most of performance issues tied to proper database design and indexing plan, and server/hardware tuning as a next level.

    0 讨论(0)
  • 2020-12-31 14:28

    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.

    0 讨论(0)
  • 2020-12-31 14:28

    According to this similar question, yes, potentially there is a big performance hit because of difference in the way DECIMAL and INT are treated and threaded into the CPU when doing calculations.

    See: Is there a performance hit using decimal data types (MySQL / Postgres)

    0 讨论(0)
提交回复
热议问题