论坛首页 Java企业应用论坛

Hibernate 与 原生 Jdbc 批量插入时的差距

浏览 20104 次
精华帖 (0) :: 良好帖 (4) :: 新手帖 (0) :: 隐藏帖 (1)
作者 正文
   发表时间:2011-05-03  
没心情全部生成完,你自己写的log:

hibernate:
18:42:38 [   20000] number with a count...
18:42:48 [   40000] number with a count...
18:42:55 [   60000] number with a count...
18:43:04 [   80000] number with a count...
18:43:11 [  100000] number with a count...
18:43:18 [  120000] number with a count...
18:43:26 [  140000] number with a count...
18:43:32 [  160000] number with a count...
18:43:41 [  180000] number with a count...
18:43:51 [  200000] number with a count...
18:43:58 [  220000] number with a count...
18:44:06 [  240000] number with a count...
18:44:15 [  260000] number with a count...
18:44:23 [  280000] number with a count...
18:44:31 [  300000] number with a count...
18:44:40 [  320000] number with a count...
18:44:49 [  340000] number with a count...
18:44:59 [  360000] number with a count...
18:45:09 [  380000] number with a count...
18:45:20 [  400000] number with a count...
18:45:33 [  420000] number with a count...
18:45:47 [  440000] number with a count...
18:45:58 [  460000] number with a count...
18:46:20 [  480000] number with a count...
18:46:49 [  500000] number with a count...
18:47:28 [  520000] number with a count...
18:48:19 [  540000] number with a count...
18:49:19 [  560000] number with a count...

jdbc:
18:55:14 [   20000] number with a count...
18:56:39 [   40000] number with a count...
18:57:18 [   60000] number with a count...
18:57:45 [   80000] number with a count...
18:57:56 [  100000] number with a count...
18:58:06 [  120000] number with a count...
18:58:20 [  140000] number with a count...
18:58:30 [  160000] number with a count...
18:58:41 [  180000] number with a count...
18:58:52 [  200000] number with a count...
18:59:01 [  220000] number with a count...
18:59:09 [  240000] number with a count...
18:59:19 [  260000] number with a count...
18:59:32 [  280000] number with a count...
18:59:45 [  300000] number with a count...
18:59:53 [  320000] number with a count...
19:00:02 [  340000] number with a count...
19:00:14 [  360000] number with a count...
19:00:21 [  380000] number with a count...
19:00:32 [  400000] number with a count...
19:00:41 [  420000] number with a count...
19:01:00 [  440000] number with a count...
19:01:31 [  460000] number with a count...
19:02:06 [  480000] number with a count...
19:02:50 [  500000] number with a count...
19:03:49 [  520000] number with a count...
19:04:36 [  540000] number with a count...
19:05:40 [  560000] number with a count...

蛋疼的数了下jdbc的时间间隔比hibernate的长一倍 这说明什么?
jdbc:     25 39 23 11 6
hibernate:10 7  9  5  7
0 请登录后投票
   发表时间:2011-05-03   最后修改:2011-05-03

core2 P8400, 4G mem

 

我的测试100W记录,mysql使用缺省安装,java -server -Xms2048M -Xmx2048M

hibernate

insert 1000000 count, consume 521.133 seconds

jdbc

insert 1000000 count, consume 508.46 seconds

 

select count(*) from APP_USER; 1000000 rows, 0.31 seconds;

 

结构APP_USER(ID, NAME, PASSWORD)

写入数据i: insert into APP_USER(NAME, PASSWORD) values(String.valueOf(i), String.valueOf(i));

 

 

可见Hibernate性能并不太慢。大约损失性能2.75%还是可以接受,如果对性能苛刻的要求建议还是直接jdbc吧。

 

  • hibernate:
    	@Test
    	@Transactional(propagation = Propagation.REQUIRES_NEW)
    	public void testHibernate() {
    		logger.info("start test Hibernate...");  
            long begin = System.currentTimeMillis();
            baseDao.saveOrUpdateAll(users);
            long end = System.currentTimeMillis();  
            logger.info("insert " + num + " count, consume " + (end - begin) / 1000f + " seconds");  
    	}
     
  • jdbc
    	@Test
    	@Transactional(propagation = Propagation.REQUIRES_NEW)
    	public void testJdbc() {
    		logger.info("start test jdbc...");  
            long begin = System.currentTimeMillis();
            jdbcTemplate.batchUpdate("insert into APP_USER(NAME, PASSWORD) values (?, ?)", new BatchPreparedStatementSetter() {
    			@Override
    			public void setValues(PreparedStatement ps, int row) throws SQLException {
    				User user = users.get(row);
    				ps.setString(1, user.getName());
    				ps.setString(2, user.getPassword());
    			}
    			@Override
    			public int getBatchSize() {
    				return users.size();
    			}
    		});
            long end = System.currentTimeMillis();  
            logger.info("insert " + num + " count, consume " + (end - begin) / 1000f + " seconds");  
    	}

不过你的hibenate和jdbc的差距也不是很大阿。

看到你测试性能损失8.37%差不多阿,不过你的这个count太慢了点吧。

 

0 请登录后投票
   发表时间:2011-05-03   最后修改:2011-05-03
何不试试bbossgroups持久层框架的预编译批处理插入和删除操作,呵呵,性能也不差哦,还有事务管理:
public String listbean(List<ListBean> beans) {
		String sql = "INSERT INTO LISTBEAN (" + "ID," + "FIELDNAME,"
				+ "FIELDLABLE," + "FIELDTYPE," + "SORTORDER,"
				+ " ISPRIMARYKEY," + "REQUIRED," + "FIELDLENGTH,"
				+ "ISVALIDATED" + ")" + "VALUES"
				+ "(#[id],#[fieldName],#[fieldLable],#[fieldType],#[sortorder]"
				+ ",#[isprimaryKey],#[required],#[fieldLength],#[isvalidated])";
		TransactionManager tm = new TransactionManager();
		try {
			tm.begin();
			SQLExecutor.delete("delete from LISTBEAN");
			SQLExecutor.insertBeans(sql, beans);
		 tm.commit();
			
		} catch (Exception e) {
			try {
				tm.rollback();
			} catch (RollbackException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}

		return "/databind/table";
	}

bbossgroups相关的几篇文章:
http://yin-bp.iteye.com/admin/blogs/1026261
http://yin-bp.iteye.com/admin/blogs/1026245
http://yin-bp.iteye.com/admin/blogs/1026054
http://yin-bp.iteye.com/blog/1004719
http://yin-bp.iteye.com/blog/1013877
0 请登录后投票
   发表时间:2011-05-04  
你的表格能说明下什么意思吗?看不太明白,那个J/30、J/50、State/30、State/50、H/30、H/50代表什么?数据库连接?
0 请登录后投票
   发表时间:2011-05-04  
事务开启了吗?
0 请登录后投票
   发表时间:2011-05-04  
hibernate就是对jdbc的封装,
根本就没什么区别,就算有区别也是在可以容忍的范围内
0 请登录后投票
   发表时间:2011-05-05  
看的我头都晕,为啥还加一堆的spirng,dao,service之类的代码  分别直接用hibernate和jdbc批量测试不行呀
0 请登录后投票
   发表时间:2011-05-05   最后修改:2011-05-05
zk1878 写道
看的我头都晕,为啥还加一堆的spirng,dao,service之类的代码  分别直接用hibernate和jdbc批量测试不行呀


因为实际的项目是构建在这样的架构下.
层次清晰, 应该还不至于那样晕!
0 请登录后投票
   发表时间:2011-05-05  
jorneyR 写道
事务开启了吗?


请注意看 applicationContext.xml
0 请登录后投票
   发表时间:2011-05-05  
Hibernate跟JDBC有啥可比的,无语~
0 请登录后投票
论坛首页 Java企业应用版

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