with rewriteBatchedStatements = true JDBC会将尽可能多的查询打包到单个网络数据包中,从而降低网络开销。我对吗?
是。下面的代码
String myConnectionString =
"jdbc:mysql://localhost:3307/mydb?" +
"useUnicode=true&characterEncoding=UTF-8";
try (Connection con = DriverManager.getConnection(myConnectionString, "root", "whatever")) {
try (PreparedStatement ps = con.prepareStatement("INSERT INTO jdbc (`name`) VALUES (?)")) {
for (int i = 1; i <= 5; i++) {
ps.setString(1, String.format(
"Line %d: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
i));
ps.addBatch();
}
ps.executeBatch();
}
}
将派个人INSERT语句即使我已经造就了一批批
INSERT INTO jdbc (`name`) VALUES ('Line 1: Lorem ipsum ...')
INSERT INTO jdbc (`name`) VALUES ('Line 2: Lorem ipsum ...')
不过,如果我更改连接字符串包括rewriteBatchedStatements=true
String myConnectionString =
"jdbc:mysql://localhost:3307/mydb?" +
"useUnicode=true&characterEncoding=UTF-8" +
"&rewriteBatchedStatements=true";
那么JDBC会发送一个或多个多行INSERT语句
INSERT INTO jdbc (`name`) VALUES ('Line 1: Lorem ipsum ...'),('Line 2: Lorem ipsum ...')
确实JDBC知道分配给max_allowed_packet的值,因此使数据包小于max_allowed_packet的定义值...?
是。如果启用MySQL通用日志并检查它,您会看到MySQL Connector/J在连接时会检查一堆变量,其中一个是max_allowed_packet
。您还可以设置一个小的max_allowed_packet
值,并验证JDBC是否将批次拆分为多个多行INSERT语句,如果整个批次的单个语句将超过max_allowed_packet
。
为什么要投票?谨慎解释? – dazito 2014-10-10 20:45:44