1

For the purpose of optimizing big insert processes in our system, we intend to batch in our insert sql statements. But no matter what I configure, so far, I'm not able to accomplish that.

Based on the answer on this SO question: Hibernate batch size confusion

It should be possible.

I already configured the hibernate.jdbc.batch_size property but still was not able to batch the insert statements? I can confirm this by enabling sql logs on both hibernate side and the postgres server side.

Is it possible to merge multiple insert statements to 1 insert statement in hibernate with postgres db?

Sample entity I used below:

class GenericMessage {
    String name
    //other fields
}

I'm using GORM, so it's on groovy (These are done in a transaction):

GenericMessage message1 = new GenericMessage(name: 'name1').save()
GenericMessage message2 = new GenericMessage(name: 'name2').save()
session.flush()
session.clear()
Community
  • 1
  • 1
froi
  • 7,268
  • 5
  • 40
  • 78
  • 1
    [Duplicate?](https://stackoverflow.com/questions/32121711/hibernate-multi-row-insert-postgresql) Not sure. Short answer seems to be that `rewriteBatchedStatements` is a MySQL specific JDBC driver parameter and that this is not relevant to Postgres - see [this message thread](https://www.postgresql.org/message-id/828427796@web.de) where the team argue that such a rewrite would be both complex and pointless. – Boris the Spider Jan 19 '17 at 14:41
  • @BoristheSpider I'm under the impression that the Postgres team likes to argue about any proposed features ;) – Kayaman Jan 19 '17 at 14:55
  • @Kayaman no idea - don't use Postgres; but if they're anything like the Java team... I suppose either way, though, there's no way to do what the OP wants. Not sure if it's a duplicate - obviously feel free to dup-hammer it if you think it is. – Boris the Spider Jan 19 '17 at 14:57
  • Can you post your code... I mean the entity that you're trying to insert and the code where your persist your entity, please? – Carlitos Way Jan 19 '17 at 18:31
  • @CarlitosWay Updated the question. Simplified it a bit. Hopefully that's ok. – froi Jan 19 '17 at 19:03
  • I'm not familiar with `GORM`, but why `GenericMessage` does not have JPA Annotations, like `@Entity` for example?? – Carlitos Way Jan 19 '17 at 19:19
  • @CarlitosWay Please assume that they are annotated with Entity. GORM just omits those in favor of convention over configuration. – froi Jan 19 '17 at 19:26
  • Ok... I'm just asking because if you read links as [this](http://stackoverflow.com/questions/12639118/jdbc-batch-performance/12648498#12648498) one... It will tell you that your batch insert will be affected by your Entity ID generation strategy... – Carlitos Way Jan 19 '17 at 19:33
  • @CarlitosWay the post also says that regardless of whether queries are batched performance won't be improved, which agrees with the data [in my comment](https://stackoverflow.com/questions/41744475/is-it-possible-to-merge-multiple-insert-statements-to-1-insert-statement-in-hibe#comment70681699_41744475). So I would suggest to the OP that this is a dead end. – Boris the Spider Jan 20 '17 at 08:25

0 Answers0