java - How to improve performance of Updating data using JPA -
i using ejb , container managed em ( local testing creating em here). have requirement need update database based on condition, issue update taking long time, how reduce ?
i tried 2 approach 1> update query 2> update in entity
please let me know if doing mistake, or other approach exist.
note : code update below
public class test { private static final int oaonaccount = 0; private static final int arraylist = 0; private static entitymanagerfactory emf; private static entitymanager em; static int test_size = 20000/4; public static void main(string[] args) { // createbulk(); createupdatequery(); // update(); } private static void createupdatequery() { long st = system.currenttimemillis(); emf = persistence.createentitymanagerfactory("jpa"); em = emf.createentitymanager(); system.out.println("---- createupdatequery ---"); entitytransaction tx = em.gettransaction(); query query = em.createquery("select p oaonaccount p"); tx.begin(); java.util.vector<oaonaccount> list = (java.util.vector<oaonaccount>) query.getresultlist(); (int = 0; < list.size(); i++) { string m = 1000000 + (i / 20) + ""; query = em .createquery("update oaonaccount p set p.status='completed', p.billingdoc='12112abcs' p.crdrindicator='h' , p.status ='open' , p.documentnumber="+ m); query.executeupdate(); } em.flush(); tx.commit(); long et = system.currenttimemillis(); system.out.println("test.createupdatequery() time " + (et - st)); } private static void update() { long st = system.currenttimemillis(); emf = persistence.createentitymanagerfactory("jpa"); em = emf.createentitymanager(); system.out.println("---- update ---"); entitytransaction tx = em.gettransaction(); query query = em.createquery("select p oaonaccount p"); tx.begin(); java.util.vector<oaonaccount> list = (java.util.vector<oaonaccount>) query .getresultlist(); (int = 0; < list.size(); i++) { string m = 1000000 + (i / 20) + ""; query = em .createquery("select p oaonaccount p p.crdrindicator='h' , p.status ='open' , p.documentnumber=" + m); java.util.vector<oaonaccount> listen = (java.util.vector<oaonaccount>) query .getresultlist(); (int j = 0; j < listen.size(); j++) { listen.get(j).setbillingdoc("12112abcs"); listen.get(j).setstatus("completed"); } } em.flush(); tx.commit(); long et = system.currenttimemillis(); system.out.println("test.update() time " + (et - st)); } public static void createbulk() { long st = system.currenttimemillis(); emf = persistence.createentitymanagerfactory("jpa"); em = emf.createentitymanager(); system.out.println("-------"); entitytransaction tx = em.gettransaction(); tx.begin(); (int = 0; < test_size; i++) { oaonaccount entity = new oaonaccount(); entity.setid("id-" + i); entity.setcrdrindicator(i % 2 == 0 ? "h" : "s"); entity.setdocumentnumber(1000000 + (i / 20) + ""); entity.setassignment(89000000 + (i / 27) + ""); entity.setstatus("open"); em.persist(entity); } em.flush(); tx.commit(); long et = system.currenttimemillis(); system.out.println("test.createbulk() time " + (et - st)); } }
you should execute em.flush() every n- number of iterations. example if n- low more number of db interactions hence slow in executing code . if n- high many objects resides in memory more swappings hence slow in executing code . please choose n value moderately , apply it. tried update 2.4 million records, faced same problem.
(int = 0; < list.size(); i++) { string m = 1000000 + (i / 20) + ""; query = em .createquery("update oaonaccount p set p.status='completed', p.billingdoc='12112abcs' p.crdrindicator='h' , p.status ='open' , p.documentnumber="+ m); query.executeupdate(); if(i%100==0){// 100 show example-- % operation costly. can use better logic flush. frequent flushing necessary em.flush(); } }
Comments
Post a Comment