NutzCN Logo
分享 关于Nutz Dao 批量插入'慢'的问题
发布于 1561天前 作者 lihongjie0209 1831 次浏览 复制 上一个帖子 下一个帖子
标签:

使用批量插入1W条数据发现很慢, 于是在Mysql中使用 set GLOBAL general_log = 0; 打开了日志, 看到每次插入都是一个独立的SQL, 并没有做插入合并.
而在Nutz的代码中是有使用批量的

        // 恩,批
            else {
                for (Object[] params : paramMatrix) {
                    for (int i = 0; i < params.length; i++) {
                        adaptors[i].set(pstat, params[i], i + 1);
                    }
                    pstat.addBatch();// 需要配置一下batchSize,嘻嘻,不然分分钟爆内存!!
                }

那么最后的问题只可能是Mysql 的JDBC的问题了, 查看MySQL JDBC源码发现有一个变量可以配置MySQL JDBC是否需要重写SQL rewriteBatchedStatements, 配置好之后就可以看到最后的SQL是批量插入的SQL了

           try {
                statementBegins();

                clearWarnings();

                if (!this.batchHasPlainStatements && this.connection.getRewriteBatchedStatements()) {

                    if (canRewriteAsMultiValueInsertAtSqlLevel()) {
                        return executeBatchedInserts(batchTimeout);
                    }

                    if (this.connection.versionMeetsMinimum(4, 1, 0) && !this.batchHasPlainStatements && this.batchedArgs != null
                            && this.batchedArgs.size() > 3 /* cost of option setting rt-wise */) {
                        return executePreparedBatchAsMultiStatement(batchTimeout);
                    }
                }

                return executeBatchSerially(batchTimeout);

参考:

https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html

https://blog.csdn.net/qq_15003505/article/details/56669256

0 回复
添加回复
请先登陆
回到顶部