聊聊jdbc的batch操做

本文主要研究一下jdbc的batch的使用以及jpa的batch設置html

batch

statement的batch操做,能夠批量進行insert或update操做,提高操做性能,特別是在大數據量的insert或update的時候。java

使用方式

@Test
    public void testSqlInjectSafeBatch(){
        String sql = "insert into employee (name, city, phone) values (?, ?, ?)";

        Connection conn = null;
        PreparedStatement pstmt = null;

        try{
            conn = dataSource.getConnection();
            conn.setAutoCommit(false);
            pstmt = conn.prepareStatement(sql);

            for (int i=0;i<3;i++) {
                pstmt.setString(1,"name"+i);
                pstmt.setString(2,"city"+i);
                pstmt.setString(3,"iphone"+i);
                pstmt.addBatch();
            }
            pstmt.executeBatch();

            conn.commit();

        }catch (SQLException e){
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }finally {
            DbUtils.closeQuietly(pstmt);
            DbUtils.closeQuietly(conn);
        }
    }
主要就是每條操做參數設置完以後,調用addBatch方法,而後再全部操做都pstmt.addBatch()完以後,調用pstmt.executeBatch()
這種方式有個缺陷就是數據量大容易消耗內存,所以建議再分批次處理
@Test
    public void testSqlInjectSafeAndOOMSafeBatch(){
        String sql = "insert into employee (name, city, phone) values (?, ?, ?)";

        Connection conn = null;
        PreparedStatement pstmt = null;

        final int batchSize = 1000;
        int count = 0;

        try{
            conn = dataSource.getConnection();
            pstmt = conn.prepareStatement(sql);

            for (int i=0;i<10000;i++) {
                pstmt.setString(1,"name"+i);
                pstmt.setString(2,"city"+i);
                pstmt.setString(3,"iphone"+i);
                pstmt.addBatch();

                //小批量提交,避免OOM
                if(++count % batchSize == 0) {
                    pstmt.executeBatch();
                }
            }

            pstmt.executeBatch(); //提交剩餘的數據

        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DbUtils.closeQuietly(pstmt);
            DbUtils.closeQuietly(conn);
        }
    }

jpa的batch設置

spring:
  jpa:
    database-platform: org.hibernate.dialect.PostgreSQLDialect
    hibernate:
      ddl-auto: update
      naming:
        implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
        physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
    show-sql: true
    properties:
      hibernate:
        format_sql: true
        jdbc:
          batch_size: 5000
          batch_versioned_data: true
        order_inserts: true
        order_updates: true
經過設置spring.jpa.properties.hibernate.jdbc.batch_size來設置批量

實例測試

@Test
    public void testJpaBatch() {
        List<DemoUser> demoUsers = new ArrayList<>();
        for(int i=0;i<10;i++){
            DemoUser demoUser = new DemoUser();
            demoUser.setPrincipal("demo");
            demoUser.setAccessToken(UUID.randomUUID().toString());
            demoUser.setAuthType(UUID.randomUUID().toString());
            demoUser.setDeptName(UUID.randomUUID().toString());
            demoUser.setOrgName(UUID.randomUUID().toString());
            demoUsers.add(demoUser);
        }
        StopWatch stopWatch = new StopWatch("jpa batch");
        stopWatch.start();
        demoUserDao.save(demoUsers);
        stopWatch.stop();
        System.out.println(stopWatch.prettyPrint());
    }

調整batch_size參數的測試結果spring

沒有設置批量
     * StopWatch 'jpa batch': running time (millis) = 21383
     -----------------------------------------
     ms     %     Task name
     -----------------------------------------
     21383  100%

     設置批量500
     StopWatch 'jpa batch': running time (millis) = 16790
     -----------------------------------------
     ms     %     Task name
     -----------------------------------------
     16790  100%

     批量1000
     StopWatch 'jpa batch': running time (millis) = 12317
     -----------------------------------------
     ms     %     Task name
     -----------------------------------------
     12317  100%

     批量5000
     StopWatch 'jpa batch': running time (millis) = 13190
     -----------------------------------------
     ms     %     Task name
     -----------------------------------------
     13190  100%

小結

jdbc的batch參數對於大數據量的新增/更新操做來講,很是有用,能夠提高批量操做的效率。sql

doc

相關文章
相關標籤/搜索