繁体   English   中英

获取服务器光标与流式传输之间的区别

[英]Difference between fetching with server cursor vs streaming

在使用服务器游标获取结果和使用流式传输获取结果之间有什么区别(行为方面)?

可以激活前者( 服务器游标 ):

  • 对于每个语句,通过在连接属性中设置useCursorFetch=truedefaultFetchSize=N (其中N是大于零的数字。)

  • 或者通过在连接上初始设置useCursorFetch=truecom.mysql.jdbc.Connection.setUseCursorFetch(true) ,然后在语句上设置java.sql.Statement.setFetchSize(N)来表示单个语句。

后者( 流媒体 )可以激活:

  • 通过设置java.sql.Statement.setFetchSize(Integer.MIN_VALUE)或通过调用statment上的com.mysql.jdbc.Statement.enableStreamingResults()查看com.mysql.jdbc.Statement.enableStreamingResults()语句。

  • 并且可能在每个语句上通过在连接属性中设置defaultFetchSize=X ,其中X是一个等于Integer.MIN_VALUE的数字。

使用这些替代方法开发时需要考虑什么? 一个好的答案可能涉及性能锁定资源分配(/ deallocation)等主题

检查Mysql的Connector / J源代码(v5.1.39):

当使用服务器游标( setUseCursorFetch(true) )并且结果集类型是TYPE_FORWARD_ONLY ,似乎“流”模式只是一个特殊情况,其中获取的块仅为1行:

////// RowDataCursor.java /////////

private void fetchMoreRows() throws SQLException {
    ...

    synchronized (this.owner.connection.getConnectionMutex()) {
        ...

        int numRowsToFetch = this.owner.getFetchSize();
        ...

        if (numRowsToFetch == Integer.MIN_VALUE) {
            // Handle the case where the user used 'old' streaming result sets

            numRowsToFetch = 1;
        }

有关RowDataCursor实例化的条件,另请参见MysqlIO.java

    //
    // Handle cursor-based fetch first
    //

    if (this.connection.versionMeetsMinimum(5, 0, 2) && this.connection.getUseCursorFetch() && isBinaryEncoded && callingStatement != null
            && callingStatement.getFetchSize() != 0 && callingStatement.getResultSetType() == ResultSet.TYPE_FORWARD_ONLY) {
        ServerPreparedStatement prepStmt = (com.mysql.jdbc.ServerPreparedStatement) callingStatement;

        boolean usingCursor = true;

MySQL 5.7文档表明使用服务器游标在服务器端生成临时表(如果大小允许,则为内存表),这可能会影响服务器性能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM