Hibernate性能分析点滴

Hibernate性能分析点滴

关键字: 性能:Hibernate batch-size hibernate.jdbc.batch_size
hibernate.jdbc.batch_size
这个属性的使用场合是批量导入数据或批量删除时使用.其实就是相当于使用PreparedStatement.executeBatch()方法..将数个sql语句一起提交获得性能上的提高. hibernate.jdbc.batch_size在hibernate.cfg.xml中设定.
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.jdbc.fetch_size">32</prop>
<prop key="hibernate.jdbc.batch_size">16</prop>
<prop key="hibernate.max_fetch_depth">1</prop>

批量导入数据代码.

// for(int i=0;i<16;i++ ){
// UserInfo u = new UserInfo();
// u.setUserName("www"+ i);
// u.setPassword("123");
// session.save(u);
// if (i%16==0){
// session.flush();//每进行完25条时flush session和清空缓存.
// session.clear();//清空缓存
// }
//
// }


//批量更新
Connection con=getSession().connection();
PreparedStatement stmt = con.prepareStatement("update Order_Detail set quantity=? where order_id = ? and Product_Id = ? and Color_ID =?");
for(int i=0;i<quantity.length;i++){
stmt.setFloat(1, NumberUtil.parseFloat(quantity[i], 0));
stmt.setInt(2, NumberUtil.parseInt(orderId, 0));
stmt.setInt(3, NumberUtil.parseInt(productId[i], 0));
stmt.setInt(4, NumberUtil.parseInt(colorId[i], 0));
stmt.addBatch();
}
stmt.executeBatch();
con.close();
}


选择jdbc方式比用Hibernate批量跟新明显快,这在理论上已经证明,下面在利用jdbc方式的同时设置批量跟新的数据量,性能上有所提高,但具体的设置值视具体数据
Connection con=getSession().connection();
PreparedStatement stmt = con.prepareStatement("update Order_Detail set quantity=? where order_id = ? and Product_Id = ? and Color_ID =?");
Long begin = System.currentTimeMillis();
for(int i=0;i<quantity.length;i++){
stmt.setFloat(1, NumberUtil.parseFloat(quantity[i], 0));
stmt.setInt(2, NumberUtil.parseInt(orderId, 0));
stmt.setInt(3, NumberUtil.parseInt(productId[i], 0));
stmt.setInt(4, NumberUtil.parseInt(colorId[i], 0));
stmt.addBatch();
if(i%80 == 0){//执行物理批量插入
stmt.executeBatch();
con.commit();
}
}
stmt.executeBatch();
con.commit();
con.close();
Long end2 = System.currentTimeMillis();
log.info("***************引入batch的批处理跟新时间*****:"+(double)(end2 - begin));
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值