<bdo id='nMcee'></bdo><ul id='nMcee'></ul>
  1. <small id='nMcee'></small><noframes id='nMcee'>

    <legend id='nMcee'><style id='nMcee'><dir id='nMcee'><q id='nMcee'></q></dir></style></legend>

  2. <i id='nMcee'><tr id='nMcee'><dt id='nMcee'><q id='nMcee'><span id='nMcee'><b id='nMcee'><form id='nMcee'><ins id='nMcee'></ins><ul id='nMcee'></ul><sub id='nMcee'></sub></form><legend id='nMcee'></legend><bdo id='nMcee'><pre id='nMcee'><center id='nMcee'></center></pre></bdo></b><th id='nMcee'></th></span></q></dt></tr></i><div id='nMcee'><tfoot id='nMcee'></tfoot><dl id='nMcee'><fieldset id='nMcee'></fieldset></dl></div>

      <tfoot id='nMcee'></tfoot>
    1. 如何提高使用 JPA 更新数据的性能

      时间:2024-05-10
    2. <legend id='Cu5GS'><style id='Cu5GS'><dir id='Cu5GS'><q id='Cu5GS'></q></dir></style></legend>

        <small id='Cu5GS'></small><noframes id='Cu5GS'>

          <bdo id='Cu5GS'></bdo><ul id='Cu5GS'></ul>
            <tfoot id='Cu5GS'></tfoot>

              • <i id='Cu5GS'><tr id='Cu5GS'><dt id='Cu5GS'><q id='Cu5GS'><span id='Cu5GS'><b id='Cu5GS'><form id='Cu5GS'><ins id='Cu5GS'></ins><ul id='Cu5GS'></ul><sub id='Cu5GS'></sub></form><legend id='Cu5GS'></legend><bdo id='Cu5GS'><pre id='Cu5GS'><center id='Cu5GS'></center></pre></bdo></b><th id='Cu5GS'></th></span></q></dt></tr></i><div id='Cu5GS'><tfoot id='Cu5GS'></tfoot><dl id='Cu5GS'><fieldset id='Cu5GS'></fieldset></dl></div>

                  <tbody id='Cu5GS'></tbody>
              • 本文介绍了如何提高使用 JPA 更新数据的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在使用 EJB 和容器管理的 EM(我在这里创建本地测试).我有一个需求,我需要根据某些情况更新数据库,我的问题是 更新需要很长时间,如何减少它?

                I am using EJB and Container managed EM ( for local testing I am creating em here). I have a requirement where I need to update database based on some condition, My issue is Update is taking very long time, how to reduce it ?

                我尝试了两种方法1> 更新查询2> 实体更新

                I tried two approach 1> Update Query 2> Update in entity

                如果我犯了任何错误,或者存在任何其他方法,请告诉我.

                Please let me know if I am doing any mistake, or any other approach exist.

                注意:更新代码如下

                    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 FROM OaOnaccount p");
                        tx.begin();
                        java.util.Vector<OaOnaccount> list = (java.util.Vector<OaOnaccount>) query.getResultList();
                        for (int i = 0; i < list.size(); i++) {
                            String m = 1000000 + (i / 20) + "";
                            query = em
                                    .createQuery("UPDATE OaOnaccount p SET p.status='COMPLETED', p.billingDoc='12112ABCS' WHERE p.crDrIndicator='H' AND p.status ='OPEN' AND 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 FROM OaOnaccount p");
                        tx.begin();
                
                        java.util.Vector<OaOnaccount> list = (java.util.Vector<OaOnaccount>) query
                                .getResultList();
                        for (int i = 0; i < list.size(); i++) {
                            String m = 1000000 + (i / 20) + "";
                            query = em
                                    .createQuery("SELECT p FROM OaOnaccount p WHERE p.crDrIndicator='H' AND p.status ='OPEN' AND p.documentNumber="
                                            + m);
                            java.util.Vector<OaOnaccount> listEn = (java.util.Vector<OaOnaccount>) query
                                    .getResultList();
                            for (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();
                
                        for (int i = 0; i < 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));
                
                    }
                
                }
                

                推荐答案

                你应该为每 n 次迭代执行 em.flush().例如,如果 n- 数据库交互的数量太少,因此执行代码的速度会变慢.如果 n- 太高,太多的对象驻留在内存中,所以更多的交换因此执行代码的速度变慢.请适度选择n值并应用.我尝试更新 240 万条记录,我遇到了同样的问题.

                you should execute em.flush() for every n- number of iterations. for example if n- too low more number of db interactions hence slow in executing code . If n- is too high too many objects resides in memory so more swappings hence slow in executing code . Please choose n value moderately and apply it. I tried update 2.4 million records, I faced same problem.

                      for (int i = 0; i < list.size(); i++) {
                        String m = 1000000 + (i / 20) + "";
                        query = em
                                .createQuery("UPDATE OaOnaccount p SET p.status='COMPLETED', p.billingDoc='12112ABCS' WHERE p.crDrIndicator='H' AND p.status ='OPEN' AND p.documentNumber="+ m);
                        query.executeUpdate();
                        if(i%100==0){// 100 to just to show example-- % operation is costly. you can use better logic to flush. frequent flushing is necessary 
                         em.flush();
                          }
                    }
                

                这篇关于如何提高使用 JPA 更新数据的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:使用 json 解析实体时休眠部分更新 下一篇:新整数与 valueOf

                相关文章

                <i id='Z1Dpa'><tr id='Z1Dpa'><dt id='Z1Dpa'><q id='Z1Dpa'><span id='Z1Dpa'><b id='Z1Dpa'><form id='Z1Dpa'><ins id='Z1Dpa'></ins><ul id='Z1Dpa'></ul><sub id='Z1Dpa'></sub></form><legend id='Z1Dpa'></legend><bdo id='Z1Dpa'><pre id='Z1Dpa'><center id='Z1Dpa'></center></pre></bdo></b><th id='Z1Dpa'></th></span></q></dt></tr></i><div id='Z1Dpa'><tfoot id='Z1Dpa'></tfoot><dl id='Z1Dpa'><fieldset id='Z1Dpa'></fieldset></dl></div>

                • <bdo id='Z1Dpa'></bdo><ul id='Z1Dpa'></ul>
              • <small id='Z1Dpa'></small><noframes id='Z1Dpa'>

                1. <legend id='Z1Dpa'><style id='Z1Dpa'><dir id='Z1Dpa'><q id='Z1Dpa'></q></dir></style></legend>

                    <tfoot id='Z1Dpa'></tfoot>