论坛首页 入门技术论坛

Hibernate性能分析点滴

浏览 1768 次
该帖已经被评为新手帖
作者 正文
   发表时间:2007-11-19  
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));
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics