如果rewriteBatchedStatements = true,则JDBC会将尽可能多的查询打包到单个网络数据包中,从而降低了网络开销。我对吗?
是。以下代码
String myConnectionString = "jdbc:mysql://localhost:3307/mydb?" + "useUnipre=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 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?" + "useUnipre=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。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)