hibernate对于主键生成器的优化

本文讨论了主键生成器的优化策略,重点介绍了hibernate中两种优化方式:hilo和pooled,这两种方式减少了数据库访问次数,提高了效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<p>Identifier generator optimization<br>For identifier generators which store values in the database, it is inefficient for<br>them to hit the database on each and every call to generate a new identifier<br>value. Instead, you'd ideally want to group a bunch of them in memory and<br>only hit the database when you have exhausted your in-memory value group.<br>This is the role of the pluggable optimizers. Currently only the two enhanced<br>generators (Section 5.1.5, “Enhanced identifier generators” support this<br>notion.<br>• none (generally this is the default if no optimizer was specified): This says<br>to not perform any optimizations, and hit the database each and every<br>request.<br>• hilo: applies a hi/lo algorithm around the database retrieved values. The<br>values from the database for this optimizer are expected to be sequential.<br>The values retrieved from the database structure for this optimizer<br>indicates the "group number"; the increment_size is multiplied by that value<br>in memory to define a group "hi value".<br>• pooled: like was discussed for hilo, this optimizers attempts to minimize<br>the number of hits to the database. Here, however, we simply store the<br>starting value for the "next group" into the database structure rather than<br>a sequential value in combination with an in-memory grouping algorithm.<br>increment_size here refers to the values coming from the database.</p>

<p>对于主键生成器的优化的主要策略是不要每次需要主键的时候都去访问一次数据库。hilo和pooled可以实现。</p>

<p>上文中所说的两种“only the two enhanced generators”是指在hibernate3.3.2中新引入的两个价钱的主键生成策略org.hibernate.id.enhanced.SequenceStyleGenerator和org.hibernate.id.enhanced.TableGenerator</p>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值