您的位置:首页 > 其它

【系统性能优化】Hibernate调优

2015-07-24 19:24 369 查看
基础系统的学生信息维护页面,查询显示学生信息,但每次查询过程都很慢,所以对该部分进行优化.

Hibernate调优是使用SSH框架很常见的问题,一般可以从以下几方面考虑:
一、数据库设计调整
二、主配置参数(ID生成策略,二级缓存,lazy加载)
三、一级缓存管理
四、事务控制策略
五、HQL优化

我们这里的优化,是对hql语句的优化,考虑使用select
newmap代替hibernate的级联查询。

先说说实体关联关系是怎么样的:

在学生实体Student中,classes是作为一个关联实体而存在的.



在数据库中表现为tb_student表中有一个classesId字段.



在班级实体classes中,institution(专业)也是作为一个关联实体而存在的.



在数据库表中表现为tb_classes表中有一个institutionId字段.



同样地,institution实体中,nodeLevel(学校层次)也是作为一个关联实体而存在的.



在数据库表中表现为tb_institution表中有一个levelId字段.



介绍完实体级联关系和数据库表结构,我们来看看真实数据.

学生表中有1.8万条数据,如下所示.



我做的是分页查询,每页10条学生信息.

优化前:
查询语句:
<span style="white-space:pre">	</span>String hql ="From Student where isDelete =:isDelete ";


查询过程:
发送150条select语句,用时28秒.
11:43:37,290 INFO [stdout] (http-localhost/127.0.0.1:8080-1)28690毫秒

优化后:
查询语句:
String hql="select new map (S.id as id,"
		+ "S.name as name,"
		+ "S.comment as comment,"
		+ "S.remark as remark,"
		+ "S.accountAddress as accountAddress,"
		+ "S.code as code,"
		+ "S.email as email,"
		+ "S.identityCardID as identityCardID,"
		+ "S.classes.className as className,"
		+ "S.classes.institution.institutionName as institutionName,"
		+ "S.classes.institution.parentInstitution.institutionName as parentInstitution,"
		+ "S.classes.institution.schoolLevel.levelName as levelName,"
		+ "S.classes.institution.id as institutionId,"
		+ "P.professionalName as professionalName "
		+ ") from Student as S  left join S.professional as P "
		+" where S.isDelete=:isDelete ";


查询过程:
共发送1条语句,用时38毫秒.

17:45:38,054 INFO [stdout] (http-localhost/127.0.0.1:8080-6)Hibernate: select count(*) as col_0_0_ from TB_Student student0_ left outerjoin TB_Professional profession1_ on student0_.professionalId=profession1_.id
wherestudent0_.isDelete=?

17:45:38,080 INFO [stdout] (http-localhost/127.0.0.1:8080-6)Hibernate: select student0_.id as col_0_0_, student0_.name as col_1_0_,student0_.comment as col_2_0_, student0_.remark as col_3_0_,student0_.accountAddress
as col_4_0_, student0_.code as col_5_0_,student0_.email as col_6_0_, student0_.entranceDate as col_7_0_,student0_.graduateSchool as col_8_0_, student0_.identityCardID as col_9_0_,classes2_.className as col_10_0_, institutio4_.InstitutionName as col_11_0_,institutio7_.InstitutionName
as col_12_0_, schoolleve10_.levelName ascol_13_0_, classes2_.institutionId as col_14_0_, profession1_.professionalNameas col_15_0_ from TB_Student student0_ left outer join TB_Professionalprofession1_ on student0_.professionalId=profession1_.id, TB_Classes
classes2_,TB_Institution institutio4_, TB_Institution institutio7_, TB_SchoolLevelschoolleve10_ where student0_.calssesId=classes2_.id andclasses2_.institutionId=institutio4_.id and institutio4_.pId=institutio7_.idand institutio4_.levelId=schoolleve10_.id
and student0_.isDelete=? limit ?

17:45:38,091 INFO [stdout] (http-localhost/127.0.0.1:8080-6)38毫秒

关于优化的分析请参考后续博客.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: