JPA/Hibernate improve batch insert performance

后端 未结 2 1973
一整个雨季
一整个雨季 2021-01-07 04:57

I have a data model that has a ONE TO MANY relationship between ONE entity and 11 other entities. These 12 entities together represent one data packet. The problem I am havi

相关标签:
2条回答
  • 2021-01-07 05:27

    Here are two good answers on the subject

    • Hibernate batch size confusion
    • How do you enable batch inserts in hibernate?

    Notice that with identity generator (it is the generator used by default with play) batch insert is disabled.

    0 讨论(0)
  • 2021-01-07 05:36

    I have managed to solve this problem by using Hibernate Sessions for each 'group' of inserts. The results are about a 7-fold reduction in time needed to save the data. Used to take approximately 2000ms to save one 'packet' and now it takes between 200ms and 300ms to do the same thing.

    Just to repeat - this is valid for Play! Framework 1.2.3 - I am not sure whether, or how this applies to other frameworks or applications that utilize Hibernate.

        Session mySession = (Session) Pressure.em().getDelegate();
    
        for(int i = 0 ; i < data.size() ; i++){
            initializeFromJsonAndSave(data.get(i), mySession);
        }
        s.flush();
        s.clear();
    

    The 'initializeFromJsonAndSave' method was changed so that, instead of calling the object's save() method, calls mySession.save(myNewObject).

    0 讨论(0)
提交回复
热议问题