设为首页 收藏本站
查看: 703|回复: 0

[经验分享] MySQL JDBC FetchSize解析

[复制链接]

尚未签到

发表于 2018-9-28 14:04:23 | 显示全部楼层 |阅读模式
  根据http://boylook.blog.51cto.com/7934327/1298634提到MySQL JDBC的fetchsize问题. 在MySQl官方文档里只提到了streaming模式和fetchALL两种模式,那么是不是就没有中间的状态呢?
  首先是看Java JDBC的API查看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一个“bug”http://bugs.mysql.com/bug.php?id=18148里说:
There is experimental support for fetching rows in batchesWhen 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>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 = readSingleRowSet3.否则rowData = new RowDataDynamic这里主要关注RowDataCursor的next实现方式: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的.


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-603379-1-1.html 上篇帖子: mysql IP地址整形转换 下篇帖子: Mysql中如何查看版本
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表