主键类型:int vs long [英] Primary Key Type: int vs long

查看:616
本文介绍了主键类型:int vs long的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有些软件商店已经使用int类型作为持久化类的主键进行了刻录。话虽这么说,并非所有的表都增长了20亿。事实上,大多数都没有。

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.

那么,你们是否只将long类型用于那些映射到可能大表的类或者每个持久化的类上课要保持一致吗?行业一致意见是什么?

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.

推荐答案

即使表没有超大,但是具有高周转率,即如果频繁删除/插入行,则长可能是有利的。当表格保持较小时,您的自动生成/顺序唯一标识符可能会增加到较高的数字。

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.

我通常使用Long,因为我的大多数人都没有注意到性能优势项目,但是由于溢出而导致的错误会非常明显!

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!

这并不是说Int对于其他人的场景不是更好的选择,例如数据处理或复杂的查询系统。只需清楚风险/收益以及它们如何影响您的特定项目。

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.

这篇关于主键类型:int vs long的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆