oracle 休眠批量插入。它会使用一个插入而不是多个插入吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6166568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 23:49:13  来源:igfitidea点击:

Hibernate Batch Insert. Would it ever use one insert instead of multiple inserts?

javaoraclehibernatebatch-file

提问by AHungerArtist

I've been looking around trying to determine some Hibernate behavior that I'm unsure about. In a scenario where Hibernate batching is properly set up, will it only ever use multiple insert statements when a batch is sent? Is it not possible to use a DB independent multi-insert statement?

我一直在环顾四周,试图确定一些我不确定的 Hibernate 行为。在正确设置 Hibernate 批处理的情况下,它是否只会在发送批处理时使用多个插入语句?是不是不能使用独立于数据库的多插入语句?

I guess I'm trying to determine if I actually have the batching set up correctly. I see the multiple insert statements but then I also see the line "Executing batch size: 25."

我想我正在尝试确定我是否真的正确设置了批处理。我看到了多个插入语句,但随后我也看到了“Executing batch size: 25”这一行。

There's a lot of code I could post but I'm trying to keep this general. So, my questions are:

我可以发布很多代码,但我试图保持这种通用性。所以,我的问题是:

1) What can you read in the logs to be certain that batching is being used?

1) 您可以从日志中读取什么内容来确定正在使用批处理?

2) Is it possible to make Hibernate use a multi-row insert versus multiple insert statements?

2) 是否可以让 Hibernate 使用多行插入与多个插入语句?

采纳答案by JB Nizet

Hibernate uses multiple insert statements (one per entity to insert), but sends them to the database in batch mode (using Statement.addBatch()and Statement.executeBatch()). This is the reason you're seeing multiple insert statements in the log, but also "Executing batch size: 25".

Hibernate 使用多个插入语句(每个实体一个插入),但以批处理模式(使用Statement.addBatch()Statement.executeBatch())将它们发送到数据库。这就是您在日志中看到多个插入语句以及“执行批量大小:25”的原因。

The use of batched statements greatly reduces the number of roundtrips to the database, and I would be surprised if it were less efficient than executing a single statement with multiple inserts. Moreover, it also allows mixing updates and inserts, for example, in a single database call.

批处理语句的使用大大减少了到数据库的往返次数,如果它比执行具有多个插入的单个语句的效率低,我会感到惊讶。此外,它还允许混合更新和插入,例如,在单个数据库调用中。

I'm pretty sure it's not possible to make Hibernate use multi-row inserts, but I'm also pretty sure it would be useless.

我很确定让 Hibernate 使用多行插入是不可能的,但我也很确定它没有用。

回答by Werzi2001

I know that this is an old question but i had the same problem that i thought that hibernate batching means that hibernate would combine multiple inserts into one statement which it doesn't seem to do.

我知道这是一个老问题,但我遇到了同样的问题,我认为 hibernate 批处理意味着 hibernate 会将多个插入合并到一个语句中,但它似乎没有这样做。

After some testing i found this answer that a batch of multiple inserts is just as good as a multi-row insert. I did a test inserting 1000 rows one time using hibernate batch and one time without. Both tests took about 20s so there was no performace gain in using hibernate batch.

经过一些测试,我发现这个答案是一批多个插入与多行插入一样好。我做了一个测试,一次使用休眠批处理插入 1000 行,一次不使用。两个测试都花费了大约 20 秒,因此使用休眠批处理没有性能提升。

To be sure i tried using the rewriteBatchedStatements option from the MySQL Connector/J which actually combines multiple inserts into one statement. It reduced the time to insert 1000 records down to 3s.

可以肯定的是,我尝试使用 MySQL Connector/J 中的 rewriteBatchedStatements 选项,该选项实际上将多个插入合并到一个语句中。它将插入 1000 条记录的时间减少到 3 秒。

So after all hibernate batch seems to be useless and a real multi-row insert to be much better. Am i doing something wrong or what causes my test results?

所以毕竟休眠批处理似乎没用,真正的多行插入要好得多。我做错了什么还是什么导致了我的测试结果?

回答by luca peano

The Oracle bulk insert collect an array of entyty and pass in a single block to the db associating to it a unic ciclic insert/update/delete.

Oracle 批量插入收集一个实体数组并将单个块传递给与它相关联的 unic ciclic 插入/更新/删除的数据库。

Is unic way to speed network throughput .

是加快网络吞吐量的 unic 方式。

Oracle suggest to do it calling a stored procedure from hibernate passing it an array of datas.

Oracle 建议这样做,从休眠调用存储过程并传递一个数据数组。

回答by luca peano

http://biemond.blogspot.it/2012/03/oracle-bulk-insert-or-select-from-java.html?m=1Is not only a software problem but infrastructural! Problem is network data flow optimization and TCP stack fragmentation. Mysql have function. You have to do something like what is described in this article. Normal transfer on network the correct volume of data is the solution

http://biemond.blogspot.it/2012/03/oracle-bulk-insert-or-select-from-java.html?m=1不仅是软件问题,还是基础设施问题!问题是网络数据流优化和 TCP 堆栈碎片。mysql有功能。您必须执行类似于本文中描述的操作。在网络上正常传输正确的数据量是解决方案

You have also to verify network mtu and Oracle sdu/tdu utilization respect data transferred between application and database

您还必须验证网络 mtu 和 Oracle sdu/tdu 使用方面的应用程序和数据库之间传输的数据