简体   繁体   English

Hibernate 性能调优技巧

[英]Hibernate performance tuning tips

I'm developing a small application, which consists of a single table.我正在开发一个小应用程序,它由一个表组成。

I am using technologies are: NetBeans 8.1 Java 8 Hibernate 4.3.x Informix Primefaces 5我使用的技术是: NetBeans 8.1 Java 8 Hibernate 4.3.x Informix Primefaces 5

I had to investigate a time to connect with Informix Hibernate, but I got it, and the application displays the list with the requested data correctly.我不得不调查与 Informix Hibernate 连接的时间,但我得到了它,并且应用程序正确显示了包含请求数据的列表。

The problem arises with the performance of Hibernate, which is very poor, especially considering that the table contains only 36000 records.问题出在Hibernate的性能上,性能很差,特别是考虑到表只有36000条记录。

On each page change takes about 6 or 7 seconds.每页更改大约需要 6 或 7 秒。

I have been researching in the official documentation of Hibernate, but can not find concrete examples to improve performance.一直在研究Hibernate的官方文档,但是找不到具体的例子来提高性能。

Herewith the application code:附上应用代码:

hibernate.cfg.xml: hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
   <property name="hibernate.dialect">org.hibernate.dialect.InformixDialect</property>
   <property name="hibernate.connection.driver_class">com.informix.jdbc.IfxDriver</property>
   <property name="hibernate.connection.url">jdbc:informix-sqli://127.0.0.1:1526/chicho:INFORMIXSERVER=ol_chicho</property>
   <!--<property name="hibernate.connection.datasource">jdbc/votacion</property>-->
   <property name="hibernate.connection.username">informix</property>
   <property name="hibernate.connection.password">informix</property>
   <property name="hibernate.connection.autocommit">true</property>
   <property name="hibernate.current_session_context_class">thread</property>
   <property name="hibernate.default_schema">informix</property>
   <property name="hibernate.show_sql">true</property>
   <property name="hibernate.hbm2ddl.auto">validate</property>
   <property name="hibernate.cache.use_second_level_cache">true</property>
   <mapping resource="pojos/Xxpuedenvotar1.hbm.xml"/>
 </session-factory>

Pojo:波乔:

package pojos;
// Generated 23/08/2016 22:07:42 by Hibernate Tools 4.3.1



/**
 * Xxpuedenvotar1 generated by hbm2java
 */
public class Xxpuedenvotar1  implements java.io.Serializable {


 private Integer nroaccionista;
 private Short estado;
 private Integer idcliente;
 private String razonSocial;
 private Short idlocalidad;
 private Short zona;
 private String calle;
 private String puerta;
 private String localidad;

public Xxpuedenvotar1() {
}


public Xxpuedenvotar1(Integer nroaccionista) {
    this.nroaccionista = nroaccionista;
}
public Xxpuedenvotar1(Integer nroaccionista, Short estado, Integer idcliente, String razonSocial, Short idlocalidad, Short zona, String calle, String puerta, String localidad) {
   this.nroaccionista = nroaccionista;
   this.estado = estado;
   this.idcliente = idcliente;
   this.razonSocial = razonSocial;
   this.idlocalidad = idlocalidad;
   this.zona = zona;
   this.calle = calle;
   this.puerta = puerta;
   this.localidad = localidad;
}

public Integer getNroaccionista() {
    return this.nroaccionista;
}

public void setNroaccionista(Integer nroaccionista) {
    this.nroaccionista = nroaccionista;
}
public Short getEstado() {
    return this.estado;
}

public void setEstado(Short estado) {
    this.estado = estado;
}
public Integer getIdcliente() {
    return this.idcliente;
}

public void setIdcliente(Integer idcliente) {
    this.idcliente = idcliente;
}
public String getRazonSocial() {
    return this.razonSocial;
}

public void setRazonSocial(String razonSocial) {
    this.razonSocial = razonSocial;
}
public Short getIdlocalidad() {
    return this.idlocalidad;
}

public void setIdlocalidad(Short idlocalidad) {
    this.idlocalidad = idlocalidad;
}
public Short getZona() {
    return this.zona;
}

public void setZona(Short zona) {
    this.zona = zona;
}
public String getCalle() {
    return this.calle;
}

public void setCalle(String calle) {
    this.calle = calle;
}
public String getPuerta() {
    return this.puerta;
}

public void setPuerta(String puerta) {
    this.puerta = puerta;
}
public String getLocalidad() {
    return this.localidad;
}

public void setLocalidad(String localidad) {
    this.localidad = localidad;
}
}

DAO:道:

package Dao;

import Interfaces.InterfazSocios;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import pojos.Xxpuedenvotar1;

/**
 *
 * @author Gustavo
*/
public class DaoSocios implements InterfazSocios {

private List<Xxpuedenvotar1> listaSocios;

@Override
public List<Xxpuedenvotar1> verTodos(Session sesion) throws Exception {
    String hql = "FROM Xxpuedenvotar1 ORDER BY NroAccionista";
    //Query consulta = sesion.createQuery(hql).setCacheable(true);
    this.listaSocios = sesion.createCriteria(Xxpuedenvotar1.class).list();
    //this.listaSocios = consulta.list();
    return this.listaSocios;
}

}

I think with these files is sufficient for analysis, since the application works well, with the exception of its slowness.我认为这些文件足以进行分析,因为该应用程序运行良好,但速度较慢。

Thank in advance for your kind attention.预先感谢您的关注。

Can try any of these or all of these depending on the configuration, availability of modifying the configuration properties.根据配置、修改配置属性的可用性,可以尝试其中的任何一个或所有这些。

  • hibernate.show_sql mark this to false, once you are done with your analysis of query formation. hibernate.show_sql将其标记为 false,一旦您完成对查询形成的分析。 This takes time to log.这需要时间来记录。
  • You can change connection pool size configuration, if you are using one您可以更改连接池大小配置,如果您使用的是
 <property name="hibernate.c3p0.min_size">5</property> <property name="hibernate.c3p0.max_size">20</property>

This helps in attaining the connection quickly from the pool.这有助于从池中快速获得连接。 Avoids time to get the connection.避免获得连接的时间。

  • If you have huge data, Please mark the respective column in JPA class with @OrderBy .如果您有大量数据,请使用@OrderBy标记 JPA 类中的相应列。

  • Hibernate search provides @SortableField to annotate the field, This indexes internally and helps in retrieving the sorted results. Hibernate 搜索提供@SortableField来注释字段,这在内部索引并有助于检索排序结果。

[EDIT] [编辑]

If Integer nroaccionista is primary key, it is indexed by default.如果Integer nroaccionista是主键,则默认对其进行索引。 Mark the column in JPA class with @Id and the DDL creation statement generated with create the index by default.@Id标记JPA 类中的列,默认情况下用create index 生成的DDL 创建语句。 Indexed column helps in better search and sort results.索引列有助于更好地搜索和排序结果。

Hope this helps.希望这可以帮助。

First of all, there are many things you can do to speed up Hibernate.首先,您可以做很多事情来加速 Hibernate。 Check out these High-Performance Hibernate Tips , or this High-Performance Hibernate video presentation .查看这些高性能 Hibernate 技巧或此高性能 Hibernate 视频演示

Now, back to your question.现在,回到你的问题。 You are using the DriverManagerConnectionProvider which only provides a rudimentary connection pooling solution.您使用的DriverManagerConnectionProvider仅提供基本的连接池解决方案。 Better use HikariCP since it's the fastest one, and it's also available via the hibernate-hikaricp Maven dependency.最好使用 HikariCP,因为它是最快的,而且它也可以通过hibernate-hikaricp Maven 依赖项使用。

Related to your statement:与您的声明相关:

The problem arises with the performance of Hibernate, which is very poor, especially considering that the table contains only 36000 records.问题出在Hibernate的性能上,性能很差,特别是考虑到表只有36000条记录。

On each page change takes about 6 or 7 seconds.每页更改大约需要 6 或 7 秒。

Why would you want to fetch 36k records in a single shot?为什么要一次性获取 36k 条记录?

You can't display them into a UI?您不能将它们显示到 UI 中? That's why we have data pagination after all.这就是我们毕竟有数据分页的原因。

Even for a batch processor, you are better off splitting the whole workload into multiple smaller data sets that allow you to avoid long-running transactions, and even split the load among multiple worker threads.即使对于批处理器,最好将整个工作负载拆分为多个较小的数据集,这样可以避免长时间运行的事务,甚至在多个工作线程之间拆分负载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM