MySQL JDBC FetchSize解析

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

根据http://boylook.blog.51cto.com/7934327/1298634提到MySQL JDBCfetchsize问题MySQl官方文档里只提到了streaming模式和fetchALL两种模式,那么是不是就没有中间的状态呢?

首先是看Java JDBCAPI查看setFetchSize:

setFetchSize(int rows) 
Givesthe JDBC driver a hint as to the number of rows that should be fetched from thedatabase when more rows are needed for this ResultSetobject.

查看的结果给出这里的rows只是一个hint,那么对于MySQL JDBC来说是如何实现的呢?

查到MySQL一个“bughttp://bugs.mysql.com/bug.php?id=18148里说:

There is experimental support for fetching rows in batches
When using Connector/J 5.0.1 along with more recent builds of the MySQL server, you can add "useCursorFetch=true" to your JDBC url parameters, and the driver will fetch rows in batches of size setFetchSize() as defined in the JDBC API.
One could also argue that the behavior _does_ follow the JDBC API, quoting from the APIDOCS for Statement.setFetchSize():
"Gives the JDBC driver a hint as to the number of rows that should be fetched from the database when more rows are needed. The number of rows specified affects only result sets created using this statement. If the value specified is zero, then the hint is ignored. The default value is zero."
It's only a _hint_. Driver vendors are allowed to ignore hints. The very reason that the wording is there is because there are quite a few vendors who can not adhere to this "contract" in all situations.

useCursorFetch

If connected to MySQL > 5.0.2, and setFetchSize() > 0 on a statement, should that statement use cursor-based fetching to retrieve rows?

false

又说是experiment,又说是hint可能被ignore,到底支持还不不支持呢?并且官方文档给出的这个参数没说到底是不是会忽略掉fetchSize.按照故事的尿性来说我该看MySQL JDBC的源码了:

1.首先判断是否可以进行cursor read

if (this.connection.versionMeetsMinimum(5, 0, 2)
 && this.connection.getUseCursorFetch()
 && isBinaryEncoded
 && callingStatement != null
 && callingStatement.getFetchSize() != 0
 && callingStatement.getResultSetType() == ResultSet.TYPE_FORWARD_ONLY)
OK,则RowData rows = new RowDataCursor

2.如果不满足,则判断是否可以进行Streaming Read

If(this.resultSetType== java.sql.ResultSet.TYPE_FORWARD_ONLY)

   && (this.resultSetConcurrency == java.sql.ResultSet.CONCUR_READ_ONLY) && (this.fetchSize == Integer.MIN_VALUE)
如果不满足则rowData = readSingleRowSet
3.否则rowData = new RowDataDynamic
 
这里主要关注RowDataCursornext实现方式:next会去调用fetchMoreRows
3.1 如果fetchsize==Integer.MIN_VALUE,则另fetchsize=1,类似stream 模式
3.2 否则回调fetchRowsViaCursor
this.sharedSendPacket.writeByte((byte) MysqlDefs.COM_FETCH);
 this.sharedSendPacket.writeLong(statementId);
 this.sharedSendPacket.writeLong(fetchSize);
 sendCommand(MysqlDefs.COM_FETCH, null, this.sharedSendPacket, true,
 null);
 while ((row = nextRow(columnTypes, columnTypes.length, true,
 ResultSet.CONCUR_READ_ONLY, false, useBufferRowExplicit, false, null)) != null) {
 fetchedRows.add(row);
}
当执行sendCommand后,看MySQL源码是如何处理的:
317 void Materialized_cursor::fetch(ulong num_rows)
 318 {
 319 THD *thd= table->in_use;
 320 
 321 int res=0;
 322 result->begin_dataset();
 323 for(fetch_limit+= num_rows; fetch_count < fetch_limit; fetch_count++)
 324 {
 325 if((res= table->file->rnd_next(table->record[0])))
 326 break;
 327 /* Send data only if the read was successful. */
 328 /*
 329 If network write failed (i.e. due to a closed socked),
 330 the error has already been set. Just return.
 331 */
 332 if(result->send_data(item_list))
 333 return;
 334 }
 335 
 336 switch(res) {
 337 case0:
 338 thd->server_status|= SERVER_STATUS_CURSOR_EXISTS;
 339 result->send_eof();
 340 break;
 341 case HA_ERR_END_OF_FILE:
 342 thd->server_status|= SERVER_STATUS_LAST_ROW_SENT;
 343 result->send_eof();
 344 close();
 345 break;
 346 default:
 347 table->file->print_error(res,MYF(0));
 348 close();
 349 break;
 350 }
 351 }

分析到这里基本上可以确定MySQL也是支持batch fetch.

本文转自MIKE老毕 51CTO博客,原文链接:http://blog.51cto.com/boylook/1308511,如需转载请自行联系原作者



相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
1月前
|
安全 Java 数据库连接
jdbc解析excel文件,批量插入数据至库中
jdbc解析excel文件,批量插入数据至库中
21 0
|
12天前
|
存储 关系型数据库 MySQL
MySQL引擎对决:深入解析MyISAM和InnoDB的区别
MySQL引擎对决:深入解析MyISAM和InnoDB的区别
28 0
|
1天前
|
SQL 存储 关系型数据库
数据库开发之mysql前言以及详细解析
数据库开发之mysql前言以及详细解析
9 0
|
25天前
|
canal 消息中间件 关系型数据库
【分布式技术专题】「分布式技术架构」MySQL数据同步到Elasticsearch之N种方案解析,实现高效数据同步
【分布式技术专题】「分布式技术架构」MySQL数据同步到Elasticsearch之N种方案解析,实现高效数据同步
74 0
|
1月前
|
安全 Java 数据库连接
jdbc实现批量给多个表中更新数据(解析Excel表数据插入到数据库中)
jdbc实现批量给多个表中更新数据(解析Excel表数据插入到数据库中)
154 0
|
1月前
|
存储 关系型数据库 MySQL
|
1月前
|
关系型数据库 MySQL 分布式数据库
PolarDB for MySQL数据库外网连接解析失败的原因可能有以下几点
【2月更文挑战第16天】PolarDB for MySQL数据库外网连接解析失败的原因可能有以下几点
24 1
|
7天前
yolo-world 源码解析(六)(2)
yolo-world 源码解析(六)
18 0
|
7天前
yolo-world 源码解析(六)(1)
yolo-world 源码解析(六)
10 0
|
7天前
yolo-world 源码解析(五)(4)
yolo-world 源码解析(五)
17 0

推荐镜像

更多