Primary Key Type: int vs long

后端 未结 4 1369
情深已故
情深已故 2020-12-14 06:51

I know some software shops have been burned by using the int type for the primary key of a persistent class. That being said, not all tables grow past 2 billions. As a matte

4条回答
  •  隐瞒了意图╮
    2020-12-14 07:30

    Long can be advantageous even if the table does not grow super large, yet has a high turnover ie if rows are deleted/inserted frequently. Your auto-generated/sequential unique identifier may increment to a high number while the table remains small.

    I generally use Long because the performance benefits are not noticeable in most of my projects, however a bug due to overflow would be very noticeable!

    That's not to say that Int is not a better option for other people's scenarios, for example for data crunching or complex query systems. Just be clear of the risks/benefits and how they impact your specific project.

提交回复
热议问题