sql批处理查询执行是否涉及服务器和客户机之间的多个数据传输?

z0qdvdin  于 2021-07-24  发布在  Java
关注(0)|答案(0)|浏览(56)

我在网上从多个来源读到的批处理查询执行一直在说,它可以将多个语句组合在一起并一次执行,从而消除了多次来回通信。
一些声称这一点的消息来源是:
https://www.tutorialspoint.com/jdbc/jdbc-batch-processing.htm#:~:text=batch%20处理%20允许%20您%20到,通信%20开销%2c%20从而%20提高%20性能。
http://tutorials.jenkov.com/jdbc/batchupdate.html
https://www.baeldung.com/jdbc-batch-processing
所有这些都谈到了单次网络跳闸等,但是通过h2或sqlite的源代码,它看起来确实像是一个接一个地执行。尽管禁用了自动提交。
例如:sqlite

final synchronized int[] executeBatch(long stmt, int count, Object[] vals, boolean autoCommit) throws SQLException {
        if (count < 1) {
            throw new SQLException("count (" + count + ") < 1");
        }

        final int params = bind_parameter_count(stmt);

        int rc;
        int[] changes = new int[count];

        try {
            for (int i = 0; i < count; i++) {
                reset(stmt);
                for (int j = 0; j < params; j++) {
                    rc = sqlbind(stmt, j, vals[(i * params) + j]);
                    if (rc != SQLITE_OK) {
                        throwex(rc);
                    }
                }

                rc = step(stmt);
                if (rc != SQLITE_DONE) {
                    reset(stmt);
                    if (rc == SQLITE_ROW) {
                        throw new BatchUpdateException("batch entry " + i + ": query returns results", changes);
                    }
                    throwex(rc);
                }

                changes[i] = changes();
            }
        }
        finally {
            ensureAutoCommit(autoCommit);
        }

        reset(stmt);
        return changes;
    }

例如:h2

public int[] executeBatch() throws SQLException {
        try {
            debugCodeCall("executeBatch");
            if (batchParameters == null) {
                // Empty batch is allowed, see JDK-4639504 and other issues
                batchParameters = Utils.newSmallArrayList();
            }
            batchIdentities = new MergedResult();
            int size = batchParameters.size();
            int[] result = new int[size];
            SQLException first = null;
            SQLException last = null;
            checkClosedForWrite();
            for (int i = 0; i < size; i++) {
                Value[] set = batchParameters.get(i);
                ArrayList<? extends ParameterInterface> parameters =
                        command.getParameters();
                for (int j = 0; j < set.length; j++) {
                    Value value = set[j];
                    ParameterInterface param = parameters.get(j);
                    param.setValue(value, false);
                }
                try {
                    result[i] = executeUpdateInternal();
                    // Cannot use own implementation, it returns batch identities
                    ResultSet rs = super.getGeneratedKeys();
                    batchIdentities.add(((JdbcResultSet) rs).result);
                } catch (Exception re) {
                    SQLException e = logAndConvert(re);
                    if (last == null) {
                        first = last = e;
                    } else {
                        last.setNextException(e);
                    }
                    result[i] = Statement.EXECUTE_FAILED;
                }
            }
            batchParameters = null;
            if (first != null) {
                throw new JdbcBatchUpdateException(first, result);
            }
            return result;
        } catch (Exception e) {
            throw logAndConvert(e);
        }
    }

从上面的代码中,我看到有多个对数据库的调用,每个调用都有自己的结果集。批处理执行实际上是如何工作的?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题