Java >> Tutorial de Java >  >> Tag >> hibernate

Cómo agrupar sentencias INSERT y UPDATE por lotes con Hibernate

Introducción

JDBC ha estado ofreciendo durante mucho tiempo soporte para procesamiento por lotes de sentencias DML. De forma predeterminada, todos los extractos se envían uno tras otro, cada uno en un viaje de ida y vuelta de red independiente. El procesamiento por lotes nos permite enviar varias declaraciones de una sola vez, lo que evita el vaciado innecesario de flujo de sockets.

Hibernate oculta las declaraciones de la base de datos detrás de una capa de abstracción de escritura posterior transaccional. Una capa intermedia nos permite ocultar la semántica de procesamiento por lotes de JDBC de la lógica de la capa de persistencia. De esta forma, podemos cambiar la estrategia de procesamiento por lotes de JDBC sin alterar el código de acceso a los datos.

Configurar Hibernate para admitir el procesamiento por lotes de JDBC no es tan fácil como debería ser, por lo que explicaré todo lo que debe hacer para que funcione.

Tiempo de prueba

Comenzaremos con el siguiente modelo de entidad:

La publicación tiene una asociación de uno a muchos con el Comentario entidad:

@OneToMany(
    cascade = CascadeType.ALL, 
    mappedBy = "post", 
    orphanRemoval = true)
private List<Comment> comments = new ArrayList<>();

O el escenario de prueba emite ambos INSERT y ACTUALIZAR declaraciones, por lo que podemos validar si JDBC se está utilizando el procesamiento por lotes:

LOGGER.info("Test batch insert");
long startNanos = System.nanoTime();
doInTransaction(session -> {
    int batchSize = batchSize();
    for(int i = 0; i < itemsCount(); i++) {
        Post post = new Post(
            String.format("Post no. %d", i)
        );
        int j = 0;
        post.addComment(new Comment(
                String.format(
                    "Post comment %d:%d", i, j++
        )));
        post.addComment(new Comment(
                String.format(
                     "Post comment %d:%d", i, j++
        )));
        session.persist(post);
        if(i % batchSize == 0 && i > 0) {
            session.flush();
            session.clear();
        }
    }
});
LOGGER.info("{}.testInsert took {} millis",
    getClass().getSimpleName(),
    TimeUnit.NANOSECONDS.toMillis(
        System.nanoTime() - startNanos
    ));

LOGGER.info("Test batch update");
startNanos = System.nanoTime();

doInTransaction(session -> {
    List<Post> posts = session.createQuery(
        "select distinct p " +
        "from Post p " +
        "join fetch p.comments c")
    .list();

    for(Post post : posts) {
        post.title = "Blog " + post.title;
        for(Comment comment : post.comments) {
            comment.review = "Blog " + comment.review;
        }
    }
});

LOGGER.info("{}.testUpdate took {} millis",
    getClass().getSimpleName(),
    TimeUnit.NANOSECONDS.toMillis(
        System.nanoTime() - startNanos
    ));

Esta prueba persistirá un número configurable de Publicación entidades, cada una de las cuales contiene dos Comentarios . En aras de la brevedad, vamos a persistir 3 Publicaciones y el dialecto tamaño de lote predeterminado:

protected int itemsCount() {
    return 3;
}

protected int batchSize() {
    return Integer.valueOf(Dialect.DEFAULT_BATCH_SIZE);
}

Soporte por lotes predeterminado

Hibernate no emplea implícitamente JDBC procesamiento por lotes y cada INSERT y ACTUALIZAR la sentencia se ejecuta por separado:

Query:{[insert into Post (title, version, id) values (?, ?, ?)][Post no. 0,0,1]} 
Query:{[insert into Comment (post_id, review, version, id) values (?, ?, ?, ?)][1,Post comment 0:0,0,51]} 
Query:{[insert into Comment (post_id, review, version, id) values (?, ?, ?, ?)][1,Post comment 0:1,0,52]} 
Query:{[insert into Post (title, version, id) values (?, ?, ?)][Post no. 1,0,2]} 
Query:{[insert into Comment (post_id, review, version, id) values (?, ?, ?, ?)][2,Post comment 1:0,0,53]} 
Query:{[insert into Comment (post_id, review, version, id) values (?, ?, ?, ?)][2,Post comment 1:1,0,54]} 
Query:{[insert into Post (title, version, id) values (?, ?, ?)][Post no. 2,0,3]} 
Query:{[insert into Comment (post_id, review, version, id) values (?, ?, ?, ?)][3,Post comment 2:0,0,55]} 
Query:{[insert into Comment (post_id, review, version, id) values (?, ?, ?, ?)][3,Post comment 2:1,0,56]}

Query:{[update Post set title=?, version=? where id=? and version=?][Blog Post no. 1,1,2,0]} 
Query:{[update Comment set post_id=?, review=?, version=? where id=? and version=?][2,Blog Post comment 1:0,1,53,0]} 
Query:{[update Post set title=?, version=? where id=? and version=?][Blog Post no. 0,1,1,0]} 
Query:{[update Comment set post_id=?, review=?, version=? where id=? and version=?][1,Blog Post comment 0:1,1,52,0]} 
Query:{[update Post set title=?, version=? where id=? and version=?][Blog Post no. 2,1,3,0]} 
Query:{[update Comment set post_id=?, review=?, version=? where id=? and version=?][3,Blog Post comment 2:0,1,55,0]} 
Query:{[update Comment set post_id=?, review=?, version=? where id=? and version=?][3,Blog Post comment 2:1,1,56,0]} 
Query:{[update Comment set post_id=?, review=?, version=? where id=? and version=?][1,Blog Post comment 0:0,1,51,0]} 
Query:{[update Comment set post_id=?, review=?, version=? where id=? and version=?][2,Blog Post comment 1:1,1,54,0]}

Configuración de hibernate.jdbc.batch_size

Para habilitar JDBC procesamiento por lotes, tenemos que configurar el hibernate.jdbc.batch_size propiedad:

Un valor distinto de cero permite el uso de actualizaciones por lotes de JDBC2 por parte de Hibernate (por ejemplo, valores recomendados entre 5 y 30)

Estableceremos esta propiedad y volveremos a ejecutar nuestra prueba:

properties.put("hibernate.jdbc.batch_size", 
    String.valueOf(batchSize()));

Esta vez, el Comentario INSERTAR las declaraciones se procesan por lotes, mientras que UPDATE las declaraciones se dejan intactas:

Query:{[insert into Post (title, version, id) values (?, ?, ?)][Post no. 0,0,1]} 
Query:{[insert into Comment (post_id, review, version, id) values (?, ?, ?, ?)][1,Post comment 0:0,0,51]} {[insert into Comment (post_id, review, version, id) values (?, ?, ?, ?)][1,Post comment 0:1,0,52]} 
Query:{[insert into Post (title, version, id) values (?, ?, ?)][Post no. 1,0,2]} 
Query:{[insert into Comment (post_id, review, version, id) values (?, ?, ?, ?)][2,Post comment 1:0,0,53]} {[insert into Comment (post_id, review, version, id) values (?, ?, ?, ?)][2,Post comment 1:1,0,54]} 
Query:{[insert into Post (title, version, id) values (?, ?, ?)][Post no. 2,0,3]} 
Query:{[insert into Comment (post_id, review, version, id) values (?, ?, ?, ?)][3,Post comment 2:0,0,55]} {[insert into Comment (post_id, review, version, id) values (?, ?, ?, ?)][3,Post comment 2:1,0,56]}

Query:{[update Post set title=?, version=? where id=? and version=?][Blog Post no. 1,1,2,0]} 
Query:{[update Comment set post_id=?, review=?, version=? where id=? and version=?][2,Blog Post comment 1:0,1,53,0]}
Query:{[update Post set title=?, version=? where id=? and version=?][Blog Post no. 0,1,1,0]} 
Query:{[update Comment set post_id=?, review=?, version=? where id=? and version=?][1,Blog Post comment 0:1,1,52,0]}
Query:{[update Post set title=?, version=? where id=? and version=?][Blog Post no. 2,1,3,0]} 
Query:{[update Comment set post_id=?, review=?, version=? where id=? and version=?][3,Blog Post comment 2:0,1,55,0]}
Query:{[update Comment set post_id=?, review=?, version=? where id=? and version=?][3,Blog Post comment 2:1,1,56,0]}
Query:{[update Comment set post_id=?, review=?, version=? where id=? and version=?][1,Blog Post comment 0:0,1,51,0]}
Query:{[update Comment set post_id=?, review=?, version=? where id=? and version=?][2,Blog Post comment 1:1,1,54,0]}

Un JDBC el lote puede apuntar a una sola tabla, por lo que cada nuevo DML La declaración dirigida a una tabla diferente finaliza el lote actual e inicia uno nuevo. Por lo tanto, no es deseable mezclar declaraciones de tabla diferentes cuando se usa SQL procesamiento por lotes.

Declaraciones de pedidos

Hibernate puede ordenar INSERT y ACTUALIZAR declaraciones usando las siguientes opciones de configuración:

properties.put("hibernate.order_inserts", "true");
properties.put("hibernate.order_updates", "true");

Mientras que la Publicación y Comentar INSERTAR las declaraciones se agrupan en consecuencia, la ACTUALIZACIÓN las sentencias aún se ejecutan por separado:

Query:{[insert into Post (title, version, id) values (?, ?, ?)][Post no. 0,0,1]} {[insert into Post (title, version, id) values (?, ?, ?)][Post no. 1,0,2]} {[insert into Post (title, version, id) values (?, ?, ?)][Post no. 2,0,3]} 
Query:{[insert into Comment (post_id, review, version, id) values (?, ?, ?, ?)][1,Post comment 0:0,0,51]} {[insert into Comment (post_id, review, version, id) values (?, ?, ?, ?)][1,Post comment 0:1,0,52]} {[insert into Comment (post_id, review, version, id) values (?, ?, ?, ?)][2,Post comment 1:0,0,53]} {[insert into Comment (post_id, review, version, id) values (?, ?, ?, ?)][2,Post comment 1:1,0,54]} {[insert into Comment (post_id, review, version, id) values (?, ?, ?, ?)][3,Post comment 2:0,0,55]} {[insert into Comment (post_id, review, version, id) values (?, ?, ?, ?)][3,Post comment 2:1,0,56]}

Query:{[update Comment set post_id=?, review=?, version=? where id=? and version=?][1,Blog Post comment 0:0,1,51,0]}
Query:{[update Comment set post_id=?, review=?, version=? where id=? and version=?][1,Blog Post comment 0:1,1,52,0]}
Query:{[update Comment set post_id=?, review=?, version=? where id=? and version=?][2,Blog Post comment 1:0,1,53,0]}
Query:{[update Comment set post_id=?, review=?, version=? where id=? and version=?][2,Blog Post comment 1:1,1,54,0]}
Query:{[update Comment set post_id=?, review=?, version=? where id=? and version=?][3,Blog Post comment 2:0,1,55,0]}
Query:{[update Comment set post_id=?, review=?, version=? where id=? and version=?][3,Blog Post comment 2:1,1,56,0]}
Query:{[update Post set title=?, version=? where id=? and version=?][Blog Post no. 0,1,1,0]} 
Query:{[update Post set title=?, version=? where id=? and version=?][Blog Post no. 1,1,2,0]} 
Query:{[update Post set title=?, version=? where id=? and version=?][Blog Post no. 2,1,3,0]}

Agregar compatibilidad con lotes de datos de versión

Está el hibernate.jdbc.batch_versioned_data propiedad de configuración que debemos establecer para habilitar ACTUALIZAR procesamiento por lotes:

Establezca esta propiedad en verdadero si su controlador JDBC devuelve recuentos de filas correctos de executeBatch(). Por lo general, es seguro activar esta opción. Hibernate luego usará DML por lotes para los datos versionados automáticamente. El valor predeterminado es falso.

Volveremos a ejecutar nuestra prueba con esta propiedad establecida también:

properties.put("hibernate.jdbc.batch_versioned_data", "true");

Ahora tanto el INSERT y la ACTUALIZACIÓN las declaraciones están correctamente agrupadas:

Query:{[insert into Post (title, version, id) values (?, ?, ?)][Post no. 0,0,1]} {[insert into Post (title, version, id) values (?, ?, ?)][Post no. 1,0,2]} {[insert into Post (title, version, id) values (?, ?, ?)][Post no. 2,0,3]} 
Query:{[insert into Comment (post_id, review, version, id) values (?, ?, ?, ?)][1,Post comment 0:0,0,51]} {[insert into Comment (post_id, review, version, id) values (?, ?, ?, ?)][1,Post comment 0:1,0,52]} {[insert into Comment (post_id, review, version, id) values (?, ?, ?, ?)][2,Post comment 1:0,0,53]} {[insert into Comment (post_id, review, version, id) values (?, ?, ?, ?)][2,Post comment 1:1,0,54]} {[insert into Comment (post_id, review, version, id) values (?, ?, ?, ?)][3,Post comment 2:0,0,55]} {[insert into Comment (post_id, review, version, id) values (?, ?, ?, ?)][3,Post comment 2:1,0,56]}

Query:{[update Comment set post_id=?, review=?, version=? where id=? and version=?][1,Blog Post comment 0:0,1,51,0]} {[update Comment set post_id=?, review=?, version=? where id=? and version=?][1,Blog Post comment 0:1,1,52,0]} {[update Comment set post_id=?, review=?, version=? where id=? and version=?][2,Blog Post comment 1:0,1,53,0]} {[update Comment set post_id=?, review=?, version=? where id=? and version=?][2,Blog Post comment 1:1,1,54,0]} {[update Comment set post_id=?, review=?, version=? where id=? and version=?][3,Blog Post comment 2:0,1,55,0]} {[update Comment set post_id=?, review=?, version=? where id=? and version=?][3,Blog Post comment 2:1,1,56,0]}
Query:{[update Post set title=?, version=? where id=? and version=?][Blog Post no. 0,1,1,0]} {[update Post set title=?, version=? where id=? and version=?][Blog Post no. 1,1,2,0]} {[update Post set title=?, version=? where id=? and version=?][Blog Post no. 2,1,3,0]}

Valor de referencia

Ahora que logramos configurar Hibernate para JDBC procesamiento por lotes, podemos comparar la ganancia de rendimiento de la agrupación de instrucciones.

  • el caso de prueba usa un PostgreSQL base de datos instalada en la misma máquina con la JVM actualmente en ejecución
  • un tamaño de lote de 50 fue elegido y cada iteración de prueba aumenta el recuento de declaraciones en un orden de magnitud
  • todas las duraciones se expresan en milisegundos
Recuento de extractos Sin duración de inserción por lotes Sin duración de actualización por lotes Duración de inserción de lote Duración de la actualización por lotes
30 218 178 191 144
300 311 327 208 217
3000 1047 1089 556 478
30000 5889 6032 2640 2301
300000 51785 57869 16052 20954


Cuantas más filas INSERTE o ACTUALIZAR , más podemos beneficiarnos de JDBC procesamiento por lotes Para la mayoría de las aplicaciones de escritura (por ejemplo, procesadores por lotes empresariales), definitivamente deberíamos habilitar JDBC procesamiento por lotes, ya que los beneficios de rendimiento pueden ser asombrosos.

  • Código disponible en GitHub.

Etiqueta Java