带有rewriteBatchedStatements = true的MySQL和JDBC [英] MySQL and JDBC with rewriteBatchedStatements=true

查看:979
本文介绍了带有rewriteBatchedStatements = true的MySQL和JDBC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在读,这里这里这里关于使用 rewriteBatchedStatements = true的优势

I've been reading around, here, here and here about the advantages of using rewriteBatchedStatements=true

如果我理解正确, rewriteBatchedStatements = true JDBC将打包尽可能多的查询到单个网络数据包中,从而降低网络开销。我是对的吗?

If I understood it right, with rewriteBatchedStatements=true the JDBC will pack as many queries as possible into a single network packet, lowering this way the network overhead. Am I right?

然后我注意到MySQL服务器中为 max_allowed_pa​​cket 定义的值可能导致查询出现问题(查询未在服务器上执行)。

Then it comes into my attention that the value defined in the MySQL server for the max_allowed_packet may cause problems with the queries (queries not being executed on the server).

所以我的第二个问题是,JDBC是否知道分配给 max_allowed_pa​​cket的值并因此使数据包小于 max_allowed_pa​​cket 的定义值,或者这是开发人员必须考虑的内容?

So my second question is, does JDBC knows the value assigned to max_allowed_packet and therefore make the packet smaller than the defined value for max_allowed_packet or that is something that the developer has to take in consideration?

如果我理解错了,请告诉我。

If I understood something wrong, please let me know as well.

推荐答案

$ b使用rewriteBatchedStatements = true的$ b

JDBC将尽可能多的查询打包到单个网络数据包中,从而降低网络开销。我是对的吗?

with rewriteBatchedStatements=true the JDBC will pack as many queries as possible into a single network packet, lowering this way the network overhead. Am I right?

是的。以下代码

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语句也是如此批处理

will send individual INSERT statements even though I have created a Batch

INSERT INTO jdbc (`name`) VALUES ('Line 1: Lorem ipsum ...')
INSERT INTO jdbc (`name`) VALUES ('Line 2: Lorem ipsum ...')

但是,如果我将连接字符串更改为包含 rewriteBatchedStatements = true

However, if I change the connection string to include rewriteBatchedStatements=true

String myConnectionString =
        "jdbc:mysql://localhost:3307/mydb?" +
        "useUnicode=true&characterEncoding=UTF-8" +
        "&rewriteBatchedStatements=true";

然后JDBC将发送一个或多个多行INSERT语句

then JDBC will send one or more multi-row INSERT statements

INSERT INTO jdbc (`name`) VALUES ('Line 1: Lorem ipsum ...'),('Line 2: Lorem ipsum ...')




JDBC知道分配给max_allowed_pa​​cket的值因此,使数据包小于max_allowed_pa​​cket的定义值...?

does JDBC knows the value assigned to max_allowed_packet and therefore make the packet smaller than the defined value for max_allowed_packet ... ?

是。如果启用MySQL常规日志并检查它,您将看到My​​SQL Connector / J在连接时检查一堆变量,其中一个是 max_allowed_pa​​cket 。您还可以设置一个小的 max_allowed_pa​​cket 值,并验证如果整个批处理的单个此类语句超过<$ c $,则JDBC会将批处理拆分为多个多行INSERT语句c> max_allowed_pa​​cket 。

Yes. If you enable the MySQL general log and check it you will see that MySQL Connector/J inspects a bunch of variables when it connects, one of which is max_allowed_packet. You can also set a small max_allowed_packet value and verify that JDBC splits a batch into several multi-row INSERT statements if a single such statement for the whole batch would exceed max_allowed_packet.

这篇关于带有rewriteBatchedStatements = true的MySQL和JDBC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆