简体   繁体   English

Hibernate,什么是最有效的id生成策略?

[英]Hibernate, what is the most efficient id generation strategy?

I need to insert many entities into the database via Hibernate. 我需要通过Hibernate将许多实体插入到数据库中。 So, I want to find the most effective algorithm for Id generation. 所以,我想找到最有效的Id生成算法。

Accordingly Hibernate Documentation exists four widely used generation strategies: 因此, Hibernate Documentation存在四种广泛使用的生成策略:

  • IDENTITY IDENTITY
  • SEQUENCE 序列
  • TABLE
  • AUTO 汽车

I should use MySQL database, so I cannot apply SEQUENCE generation strategy. 我应该使用MySQL数据库,所以我不能应用SEQUENCE生成策略。 What about other strategies? 其他策略怎么样? What is the most efficient from performance point of view? 从性能的角度来看效率最高的是什么?

The best id generators in Hibernate are enhanced-table and enhanced-sequence , coupled with an appropriate optimizer, such as hilo . Hibernate中最好的id生成器是增强表增强序列 ,再加上适当的优化器,如hilo I have experience with enhanced-table + hilo , inserting over 10,000 records per second. 我有增强表 + hilo的经验, 每秒插入超过10,000条记录。

BTW the statement that "hilo needs an additional query per generated entity" is patently false: the whole point of the optimizer is to prevent this. BTW声明“hilo每个生成的实体需要一个额外的查询”显然是错误的:优化器的重点是防止这种情况。

As you can't use SEQUENCE , and AUTO just automatically selects a supported generator algorithm out of the existing ones, you are left with IDENTITY and TABLE . 由于您无法使用SEQUENCE ,并且AUTO只是自动选择现有支持的生成器算法,因此您将使用IDENTITYTABLE

  • TABLE : uses a hi/lo algorithm to efficiently generate identifiers of type long, short or int, given a table and column as a source of hi values. TABLE :使用hi / lo算法有效地生成long,short或int类型的标识符,给定表和列作为hi值的来源。 The hi/lo algorithm generates identifiers that are unique only for a particular database. hi / lo算法生成仅对特定数据库唯一的标识符。 -> Means an extra query per generated entity . - >表示每个生成的实体额外查询 (This is not true if you use optimizers. Unfortunately, using no optimizer generally is the default, if no optimizer was specified.) (如果使用优化器,情况就不是这样。不幸的是,如果没有指定优化器,通常不使用优化器是默认值。)

  • IDENTITY : supports identity columns in DB2, MySQL, MS SQL Server, Sybase and HypersonicSQL. IDENTITY :支持DB2,MySQL,MS SQL Server,Sybase和HypersonicSQL中的标识列。 -> Performance-wise, this is the way to go , the same way you would do without Hibernate normally. - > 性能方面,这是要走的路 ,就像没有Hibernate一样。 Database generated, almost no overhead. 数据库生成,几乎没有开销。

There exist more Hibernate specific generators, but they won't beat performance-wise the database generated ID. 存在更多Hibernate特定的生成器,但它们不会在性能方面击败数据库生成的ID。 (See 5.1.2.2.1. Various additional generators in your linked document.) (参见5.1.2.2.1. Various additional generators链接文档中的5.1.2.2.1. Various additional generators 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM