GBase 8a集群JDBC 批量提交rewriteBatchedStatements

注意:

1、必须是insert values方式,不能是insert value方式,注意多了一个字母s。

2、insert values时的语句最后,不要有分号,否则会从第二行开始出语法错误。

普通入库方式

如果你有大量的单行数据要入库,可以采用如下几个方案

1、insert方式,每次一行

2、insert 方式,每次1000行,批量提交

3、输出成文件,load加载方式

其中第一种性能最差。而第二种,本质上类似如下的操作

set autocommit=0;
insert into XXX values ();
insert into XXX values ();
insert into XXX values ();
....
commit;

这种方案性能比逐行的性能要好很多。 另外一个方案就是

insert into XXX values (),(),(),();

也就是通过一个SQL语句,多个值,写到1个SQL里面,一次执行。

批量入库方式

gbase 的jdbc驱动,通过rewriteBatchedStatements=true参数,可以自动将Batch Insert 方式的批量提交,自动重写成多个values的格式,来提高性能。

但缺点也存在,如果你的数据里存在了单引号等,没有提前转义(比如转义成\'),会导致拼接出来的SQL语法错误。

public class TestBatchInsert {

	public static void main(String[] args) {
		Connection conn;
		try {
			conn = DriverManager.getConnection(
					"jdbc:gbase://192.168.56.1:52582/testdb?user=gbase&password=gbase20110531&rewriteBatchedStatements=true&profileSql=true");

			conn.setAutoCommit(false);
			String sql = "insert into t1 (id,id2,id3,id4) values (?,?,?,?)";
			PreparedStatement prest = conn.prepareStatement(sql);

			for (int id = 1; id <= 10; id++) {
				prest.setInt(1, id);
				prest.setInt(2, id);
				prest.setInt(3, id);
				prest.setInt(4, id);
				prest.addBatch();
			}
			prest.executeBatch();
			conn.commit();

			conn.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}

一个更完整的例子截图

GBase 8a集群JDBC 批量提交rewriteBatchedStatements》有1条评论

评论已关闭。