NutzCN Logo
分享 测试nutz数据的插入时间结果
发布于 2536天前 作者 Saviour 1873 次浏览 复制 上一个帖子 下一个帖子
标签:

测试表结构如下

	@Name
	@Column(hump = true)
	@ColDefine(type = ColType.CHAR, width = 10)
	@Comment("节假日期")
	private String date;
	@Column(hump = true)
	@ColDefine(type = ColType.INT, width = 3)
	@Comment("类型 0-周末 1-节日")
	private int restdayType;
	@Column(hump = true)
	@ColDefine(type = ColType.TIMESTAMP)
	@Comment("创建时间")
	private Date createTime;

org.nutz.dao.Dao.insert(),java.sql.Statement.execute()和java.sql.Statement.executeBatch()三种方式分别插入1000条和10000条数据
1.Dao.insert()

		long start = System.currentTimeMillis();
		Calendar c = Calendar.getInstance();
		RestdayTable restday = new RestdayTable();
		restday.setRestdayType(type);
		for (int i = 0; i < 10000; i++) {
			c.add(Calendar.DAY_OF_MONTH, 1);
			restday.setDate(sdf.format(c.getTime()));
			restday.setCreateTime(new Date());
			dao().insert(restday);
		}
		long end = System.currentTimeMillis();
		System.out.println("time is " + (end - start) + "ms");

1000
time is 2652ms
10000
time is 24188ms
2.Statement.execute()

		try {
			Connection con = dataSource.getConnection();
			java.sql.Statement s = con.createStatement();
			long start = System.currentTimeMillis();
			Calendar c = Calendar.getInstance();
			StringBuilder sql = new StringBuilder("insert into restday values('2017-12-14',1,'2017-01-01 00:00:00')");
			for (int i = 0; i < 1000; i++) {
				c.add(Calendar.DAY_OF_MONTH, 1);
				sql.replace(28, 38, sdf.format(c.getTime()));
				s.execute(sql.toString());
			}
			long end = System.currentTimeMillis();
			System.out.println("time is " + (end - start) + "ms");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

1000
time is 2381ms
10000
time is 22363ms
3.Statement.executeBatch()
1000
time is 1827ms
10000
time is 14349ms

技术水平有限 测试方式可能不严谨的地方。每个用例跑了10次左右,结果相差不大,执行时间上大概就是org.nutz.dao.Dao.insert()>java.sql.Statement.execute()>java.sql.Statement.executeBatch()。
我猜可能是Dao.insert()在插入之前做过一些校验花费了不少时间(没看过源码),如果有大神能说明原理的还希望在评论区不吝赐教

1 回复

漏了3的代码
3.Statement.executeBatch()

		try {
			Connection con = dataSource.getConnection();
			java.sql.Statement s = con.createStatement();
			long start = System.currentTimeMillis();
			Calendar c = Calendar.getInstance();
			StringBuilder sql = new StringBuilder("insert into restday values('2017-12-14',1,'2017-01-01 00:00:00')");
			for (int i = 0; i < 1000; i++) {
				c.add(Calendar.DAY_OF_MONTH, 1);
				sql.replace(28, 38, sdf.format(c.getTime()));
				s.addBatch(sql.toString());
			}
			s.executeBatch();
			long end = System.currentTimeMillis();
			System.out.println("time is " + (end - start) + "ms");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
添加回复
请先登陆
回到顶部