Primary Key Type: int vs long

Tom Tucker picture Tom Tucker · Nov 7, 2010 · Viewed 7.9k times · Source

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 matter of fact, most don't.

So, do you guys use the long type only for those classes that are mapped to potentially large tables OR for every persistent class just to be consistent? What's the industry concensus?

I'll leave this question open for a while so that you can share with us your success/horror stories.

Answer

pstanton picture pstanton · Nov 7, 2010

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.