具有OneToMany/可为空的关系的休眠性能问题 [英] Hibernate performance issue with OneToMany / nullable relationship

查看:171
本文介绍了具有OneToMany/可为空的关系的休眠性能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们将@OneToMany用于我们的Parent-> Child-> Child-> Child DB关系:

We use @OneToMany for our Parent->Child->Child->Child DB relationship:

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "THE_ID", nullable = false )
private List<ChildClass> children = new ArrayList<ChildClass>();

我们有一个场景,其中包含大量数据(插入100K),插入时性能非常糟糕(实际上超时).只需少量数据(插入1K数据),我们就可以了.

We have a scenario with a lot of data (100K inserts) where the performance is atrocious (actually times out) when inserting. With a small amount of data (1K inserts) we are however fine.

因此,没有充分的理由,我删除了nullable = false并更改了子表上的数据库外键以允许为空,并保证了性能相当不错.谁能解释一下?

So for no good reason I have removed the nullable = false and changed the DB foreign key on the child tables to allow nulls and presto the performance is quite good. Can anyone explain this?

更新:打开调试..对于nullable = false,似乎为子表生成ID的瓶颈很大.我们超时等待id生成,并一遍又一遍地记录在日志中:

Update: Turned on debugging.. With nullable = false there appears to be a huge bottleneck generating the ids for the child tables. We timeout waiting for the ids to generate, with this in the log over and over:

[org.hibernate.event.def.AbstractSaveEventListener] [ ] generated identifier: <743088>, using strategy: org.hibernate.id.IncrementGenerator

我们甚至从未将数据插入到数据库中.我们只是停留在id gen上.当前,我们将Hibernate配置为通过查看子表中当前的最大ID值来生成ID:

We never even get to inserting the data to the DB. We are just stuck on the id gen. Currently we configure Hibernate to generate IDs by looking at the max id values currently in the child table:

@Id
@GeneratedValue(generator = "DummyString")
@GenericGenerator(name = "DummyString", strategy = "increment")
@Column(name = "THE_ID", nullable = false)
private Long id;

在此之前,我们使用DB序列,并且遇到了相同的问题.

Prior to this we were using a DB sequence and saw the same issues.

当我们省略nullable = false时,我们确实看到了这些ID gen语句(它们中的108K),但是它们在25秒内完成.那么,为什么这些语句(从字面上看)永远被nullable = false占据呢?

When we omit nullable = false, we do see these these ID gen statements (108K of them), but they complete in 25 seconds. So why do these statements (literally) take forever with nullable = false?

推荐答案

Voodoo吗? 了解如何执行Oracle跟踪.您的DBA应该会提供帮助.

Voodoo ? Find out how to perform Oracle tracing. Your DBA should help.

使用跟踪文件,您可以查看Oracle的等待事件.这将告诉您性能非常糟糕"时数据库正在做什么.它可能正在等待锁,可能正在读取表....

With a trace file, you can look at Oracle's wait events. This will tell you what the database was doing when 'performance is atrocious'. It could be waiting on a lock, it could be reading a table....

一旦您知道为什么它很慢,解决方案通常就会变得显而易见.

Once you know why it is slow, the solution often becomes obvious.

我推荐此

I recommend this whitepaper as an instruction to developers on working with databases.

这篇关于具有OneToMany/可为空的关系的休眠性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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