<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 更新数据的性能

      How to improve performance of Updating data using JPA(如何提高使用 JPA 更新数据的性能)
    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 更新数据的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

                <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'>

                        <tbody id='Z1Dpa'></tbody>

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

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