主键类型:int 与 long

发布于 2024-10-01 10:12:49 字数 190 浏览 3 评论 0原文

我知道有些软件商店因使用 int 类型作为持久类的主键而被烧毁。话虽如此,并非所有表的数量都会超过 20 亿。事实上,大多数人都没有。

那么,你们是否只对那些映射到潜在大表的类使用 long 类型,或者只是为了保持一致而对每个持久类使用 long 类型?行业共识是什么?

我将暂时保留这个问题,以便您可以与我们分享您的成功/恐怖故事。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

行至春深 2024-10-08 10:12:49

即使表不会变得非常大,但周转率很高(即,如果频繁删除/插入行),Long 也可能是有利的。您的自动生成/顺序唯一标识符可能会增加到一个很大的数字,而表仍然很小。

我通常使用 Long,因为在我的大多数项目中性能优势并不明显,但是由于溢出而导致的错误将非常明显!

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

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.

千笙结 2024-10-08 10:12:49

我不知道“烧伤”是什么意思。当需要时从 int 更改为 long 并不困难。 SQL 中的转换是直接进行的,然后只需在客户端代码中进行搜索和替换(或者在持久层中进行更改,然后编译并查看发生了什么问题。)您正在从一种整数类型转换为另一种整数类型,所以你不必担心微妙的转换问题或截断。

从 float 到 double 会困难得多。

I don't know about "burned". It's not difficult to change from int to long when you need to. The conversion is straight forward in SQL, and then it's just a search and replace in your client code (or make the change in your persistence layer, and then compile and see what breaks.) You're moving from one integer type to another, so you don't have to worry about subtle conversion issues or truncation..

Going from float to double would be a lot harder.

迷乱花海 2024-10-08 10:12:49

我使用 Integer 作为我的代理键,除非我需要它们是其他东西。如果不需要,则不必总是使用 Long。

(我通常在针对 Oracle 10g 或 MySQL 5.x 数据库运行的项目中使用 JPA/Hibernate。)

I use Integer for my surrogate keys unless I have a need for them to be something else. It is not necessary to always use a Long if you don't have a need for it.

(I typically use JPA/Hibernate in my projects running against either Oracle 10g or MySQL 5.x databases.)

因为 Int 对于选择/排序来说总是更快。

Because Int will always be faster for Select/Sorts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文