您的位置:首页 > 其它

Hibernate JDBC比较及系统调优

2012-12-30 22:18 323 查看
Hibernate与JDBC比较:

第一:Hibernate作为ORM,有一个ORM固有的问题,就是由于为了持久对象的同步,不能够使用批量删除和批量更新的sql,只能按照主键一条条来操作。因此效率相对JDBC来说是比较低的。然而事情也并不总是那么绝望,只要你对Hibernate进行优化,也可以得到相当满意的速度。
[code:1] session.delete("from Cat as c where ...");[/code:1]
该语句实际上发送sql:

[code:1]==> select id,name,sex,weight from cat;

==> delete from cat where id = ?[/code:1]
Hibernate先查询数据,确实要消耗一些时间,但是select只读操作和insert,delete,update这些数据库修改操作在速度上有一个以上的数量级的差距。所以 Hibernate虽然查询数据要多耗时,但是消耗的这点时间影响不是很大,主要是内存消耗的多。而delete的速度,我们知道调节Hibernate的Batch
Size可以提供大大提高insert, delete和update的速度。
我的测试:
Oracle817,ojdbc14.jar 表记录1万条,全部删除。
JDBC:
sql语句 [code:1]delete from cat[/code:1]

速度:平均6s
Hibernate:
[code:1]session.delete("from Cat as c");[/code:1]
Batch Size = 0 速度: 25s

Batch Size = 50 速度: 6s
批量删除和批量更新建议用JDBC,这是一个原则,当然有的时候可能必须用Hibernate来批量更新和批量删除,那么这个时候我想说的就是,Hibernate批量更新和删除效率并非传说中的那么差,只要优化的好,速度也非常快。(这是摘自www.javaeye.com上的,不让转载,强制了,学习吗,呵呵)
第二:2,注意在批量更新的时候,要及时的用session.clear()清缓存的东西 ,或控制session.flush,
第三:,还有就是用jtds驱动
第四:,1、针对oracle数据库而言,Fetch Size 是设定JDBC的Statement读取数据的时候每次从数据库中取出的记录条数,一般设置为30、50、100。Oracle数据库的JDBC驱动默认的Fetch Size=15,设置Fetch Size设置为:30、50,性能会有明显提升,如果继续增大,超出100,性能提升不明显,反而会消耗内存。

即在hibernate配制文件中进行配制:

1 <property name="hibernateProperties">

2 <props>

3 <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>

4 <prop key="hibernate.show_sql">false</prop>

5 <!-- Create/update the database tables automatically when the JVM starts up

6 <prop key="hibernate.hbm2ddl.auto">update</prop> -->

7 <!-- Turn batching off for better error messages under PostgreSQL

8 <prop key="hibernate.jdbc.batch_size">100</prop> -->

9 <prop key="hibernate.jdbc.batch_size">50</prop>

10 </props>

11 </property>Fetch Size设的越大,读数据库的次数越少,速度越快;Fetch Size越小,读数据库的次数越多,速度越慢。

2、如果是超大的系统,建议生成htm文件。加快页面提升速度。

3、不要把所有的责任推在hibernate上,对代码进行重构,减少对数据库的操作,尽量避免在数据库查询时使用in操作,以及避免递归查询操作,代码质量、系统设计的合理性决定系统性能的高低。

4、 对大数据量查询时,慎用list()或者iterator()返回查询结果,

(1). 使用List()返回结果时,Hibernate会所有查询结果初始化为持久化对象,结果集较大时,会占用很多的处理时间。

(2). 而使用iterator()返回结果时,在每次调用iterator.next()返回对象并使用对象时,Hibernate才调用查询将对应的对象初始化,对于大数据量时,每调用一次查询都会花费较多的时间。当结果集较大,但是含有较大量相同的数据,或者结果集不是全部都会使用时,使用iterator()才有优势。

5、在一对多、多对一的关系中,使用延迟加载机制,会使不少的对象在使用时方会初始化,这样可使得节省内存空间以及减少数据库的负荷,而且若PO中的集合没有被使用时,就可减少互数据库的交互从而减少处理时间。

6、对含有关联的PO(持久化对象)时,若default-cascade="all"或者 “save-update”,新增PO时,请注意对PO中的集合的赋值操作,因为有可能使得多执行一次update操作。

7、 对于大数据量新增、修改、删除操作或者是对大数据量的查询,与数据库的交互次数是决定处理时间的最重要因素,减少交互的次数是提升效率的最好途径,所以在开发过程中,请将show_sql设置为true,深入了解Hibernate的处理过程,尝试不同的方式,可以使得效率提升。尽可能对每个页面的显示,对数据库的操作减少到100----150条以内。越少越好。

Hibernate批理更新与批量删除
批理修改:
场景:如有一个学生表Student,现有一属性[学院]改名,从"计算机学院"改为"计算机工程学院"[不考虑学院表].
用Hibernate实现这种批理更新的方法一DML(数据操作语言)操作。代码如下:

publicvoid
updateUser(String newName,String oldName){

Session session =
null;

try{

session =
this.getSession();

Transaction tc
= session.beginTransaction();

String hqlUpdate
= "update Student set deptName=:newName where deptName= :oldName";

int updatedEntities
= s.createQuery( hqlUpdate)

.setString("newName", newName)

.setString("oldName", oldName)

.executeUpdate();

tc.commit();

}catch(RuntimeException re){

log.debug(re.getMessage(),re);

throw re;

}finally{

if (session!=null){

session.close();

}

}

}

方法二绕过Hibernate API,用JDBC实现。
publicvoid
UpdateUser(String newName,String oldName){

Session session =
null;

try{

session =
this.getSession();

Transaction tc
= session.beginTransaction();

Connection
connection = session.connection();

PreparedStatement ps
= connection.prepareStatement("update Student set deptName='"+newName+"'
where deptName= '"+oldName+"'");

ps.execute();

tc.commit();

}catch(RuntimeException re){

log.debug(re.getMessage(),re);

throw re;

}catch(SQLException e){

log.debug(e.getMessage(),e);

}finally{

if (session!=null){

session.close();

}

}

}

批量删除
场景:如有一个学生表Student,现要删除学院为"技术学院"的学生.
用Hibernate实现这种批理删除的方法一DML(数据操作语言)操作。代码如下:
publicvoid
deleteUser(String deptName){

Session session =
null;

try{

session =
this.getSession();

Transaction tc
= session.beginTransaction();

String hqlUpdate
= "delete from Student where deptName= :deptName";

int updatedEntities
= s.createQuery( hqlUpdate)

.setString("deptName", deptName)

.executeUpdate();

tc.commit();

}catch(RuntimeException re){

log.debug(re.getMessage(),re);

throw re;

}finally{

if (session!=null){

session.close();

}

}

}

方法二绕过Hibernate API,用JDBC实现。
publicvoid
deleteUser(String deptName){

Session session =
null;

try{

session =
this.getSession();

Transaction tc
= session.beginTransaction();

Connection
connection = session.connection();

PreparedStatement ps
= connection.prepareStatement("delete from Student where
deptName= '"+deptName+"'");

ps.execute();

tc.commit();

}catch(RuntimeException re){

log.debug(re.getMessage(),re);

throw re;

}catch(SQLException e){

log.debug(e.getMessage(),e);

}finally{

if (session!=null){

session.close();

}

}

}

使用存储过程

如果底层数据库(如Oracle)支持存储过程,也可以通过存储过程来执行批量更新。存储过程直接在数据库中运行,速度更加快。
createor
replace procedure updateProject is

begin

update project p
set p.total_intend_gather
=

(selectsum(ig.gather_sum)from
intend_gather ig where ig.project_number=p.project_number);

update project p
set p.total_actual_gather
=

(selectsum(ag.gahter_sum)from
actual_gather ag where ag.project_number=p.project_number);

update project p
set p.total_invoice=

(selectsum(invoice.invoice_sum)from
invoice invoice

where invoice.intend_id
in

(select ig.intend_idfrom intend_gather ig where ig.project_number=p.project_number));

end updateProject;

调用代码
Session session=
this.getSession();

Transaction tx
=null;

try {

tx = session.beginTransaction();

Connection con
= session.connection();

String procedure
= "{call updateproject() }";

CallableStatement cstmt
= con.prepareCall(procedure);

cstmt.executeUpdate();

tx.commit();

} catch(Exception e){

tx.rollback();

}

Hibernate作为一个O/R Mapping,比JDBC具备的优势有:

1.编程思想上,更加符合人的逻辑思维习惯,面向对象比面向过程更加容易理解,测试和维护

2.开发维护速度上,Hibernate显著的快,代码量显著小

3.通过Annotation进行数据库的字段加密

4.对Sql不熟的菜鸟来说可以自动调优

5.结合Spring,通过声明式事务可以省略事务的控制,事务以横切面形式出现

Jdbc比Hibernate具备的优势有:

1.大数据量访问时,Jdbc的效率显著快

2.直接操作数据库比较灵活

Hibernate比Jdbc慢的地方:

1.将Hql语句转化为标准Sql语句,花费时间可以忽略不急

2.将ResultSet中数据组装为实体对象,如果即所得数据量比较大(比如一次检索1000条数据),这是效率就会降低,在组装实体对象花费时间较长

检索单表的1000条数据,Hibernate与Jdbc均不会超过1S钟.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: