solr官方给出的解决方式是:

DataImportHandler is designed to stream row one-by-one. It passes a fetch size value (default: 500) to Statement#setFetchSize which some drivers do not honor. For MySQL, add batchSize property to dataSource configuration with value -1. This will pass Integer.MIN_VALUE to the driver as the fetch size and keep it from going out of memory for large tables.Should look like:<dataSource type="JdbcDataSource" name="ds-2" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:8889/mysqldatabase" batchSize="-1" user="root" password="root"/>

说明:DataImportHandler 设计是支持按行获取的。它通过Statement#setFetchSize来设置每次获取的数量,默认是500个。然而一些驱动不支持设置fetchSize。对mysql来说,传递fetchSize属性值-1到Datasource配置中。它将将Integer.MIN_VALUE(-231,-2147483648 [0x80000000])传给驱动作为fetchsize,此时确保大表不会造成大表移除。

mysql官方给出的解释是:

ResultSetBy default, ResultSets are completely retrieved and stored in memory. In most cases this is the most efficient way to operate and, due to the design of the MySQL network protocol, is easier to implement. If you are working with ResultSets that have a large number of rows or large values and cannot allocate heap space in your JVM for the memory required, you can tell the driver to stream the results back one row at a time.To enable this functionality, create a Statement instance in the following manner:stmt = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY,java.sql.ResultSet.CONCUR_READ_ONLY);
stmt.setFetchSize(Integer.MIN_VALUE);
The combination of a forward-only, read-only result set, with a fetch size of Integer.MIN_VALUE serves as a signal to the driver to stream result sets row-by-row. After this, any result sets created with the statement will be retrieved row-by-row.There are some caveats with this approach. You must read all of the rows in the result set (or close it) before you can issue any other queries on the connection, or an exception will be thrown.The earliest the locks these statements hold can be released (whether they be MyISAM table-level locks or row-level locks in some other storage engine such as InnoDB) is when the statement completes.If the statement is within scope of a transaction, then locks are released when the transaction completes (which implies that the statement needs to complete first). As with most other databases, statements are not complete until all the results pending on the statement are read or the active result set for the statement is closed.Therefore, if using streaming results, process them as quickly as possible if you want to maintain concurrent access to the tables referenced by the statement producing the result set.

通过联合使用forward-only,read-only resultSet和fetchsize值为Integer.MIN_VALUE作为驱动一行行获取结果流的信号。设置完以后,所有statement创建的resultSet将会一行行的获取结果集。

参考文献:

【1】 https://wiki.apache.org/solr/DataImportHandlerFaq

【2】http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html

转载于:https://www.cnblogs.com/davidwang456/p/4800911.html

使用solr的DIHandler 构建mysql大表全量索引,内存溢出问题的解决方法相关推荐

  1. php mysql 内存溢出_关于MySQL的整型数据的内存溢出问题的应对方法_MySQL

    今天接到一个朋友电话说是觉的数据库被别人更改了,出现数据不对的问题 .经过很久的排查是数据类型溢出了(发生问题的版本是MySQL 5.1).后来通过给朋友那边把MySQL 5.1升级到MySQL 5. ...

  2. MySQL大表历史数据迁移这么做

    历史数据迁移 项目地址:https://gitee.com/xl-echo/dataMigration 历史迁移解决方案.微服务的架构为基础,使用多种设计模式,如:单利.桥接.工厂.模板.策略等.其中 ...

  3. 【开源项目】mysql大表数据迁移

    历史数据迁移 项目地址:https://gitee.com/xl-echo/dataMigration 历史迁移解决方案.微服务的架构为基础,使用多种设计模式,如:单利.桥接.工厂.模板.策略等.其中 ...

  4. 解决MYSQL大表问题-实战篇(二)

    #首先上表结构 CREATE TABLE `sys_history` (`id` bigint(20) NOT NULL AUTO_INCREMENT,`did` bigint(20) NOT NUL ...

  5. delete mysql 大表_无语了,直到今天,我才揪出MySQL磁盘消耗迅猛的“真凶”!

    作者:dbapower 链接:https://blog.51cto.com/suifu/2135599 背景 Part1:写在最前 当一张单表10亿数据量的表放在你面前,你将面临着什么? Part2: ...

  6. 基于Solr DIH实现MySQL表数据全量索引和增量索引

    实现MySQL表数据全量索引和增量索引,基于Solr DIH组件实现起来比较简单,只需要重复使用Solr的DIH(Data Import Handler)组件,对data-config.xml进行简单 ...

  7. 史上最全MySQL 大表优化方案(长文)

    转载自  史上最全MySQL 大表优化方案(长文) 当MySQL单表记录数过大时,增删改查性能都会急剧下降,可以参考以下步骤来优化: 一.单表优化 除非单表数据未来会一直不断上涨,否则不要一开始就考虑 ...

  8. MySQL 大表优化方案(1)

    转载自  干货!!!MySQL 大表优化方案(1) 当MySQL单表记录数过大时,增删改查性能都会急剧下降,可以参考以下步骤来优化: 单表优化 除非单表数据未来会一直不断上涨,否则不要一开始就考虑拆分 ...

  9. mysql 大表 备份_MySQL大表备份的简单方法

    MySQL大表备份是一个我们常见的问题,下面就为您介绍一个MySQL大表备份的简单方法,希望对您学习MySQL大表备份方面能有所帮助. 这里所说的大表是超过4G以上的表,我目前见到过最大为60多G的单 ...

最新文章

  1. C 内存free()出错
  2. InsecureRequestWarning: Unverified HTTPS request is being made to host(requests证书警告)
  3. mysql数据库文件的真实的物理存储位置
  4. linux第一次防火墙
  5. [bzoj2243][SDOI2011]染色
  6. 香辣弹簧:不同的自动接线方式
  7. linux rm 命令删除文件恢复_Linux之恢复误删除文件
  8. reids的持久化 RDB和AOF
  9. 架构设计(3)---基于Nginx和Zookeeper实现Dubbo的分布式服务
  10. Uniswap 24小时交易量9.7亿美元,占以太坊上Dex总量的54%
  11. 【NOI2001】【Vijos1531】食物链(并查集拓展域)
  12. 以太坊2.0怎么挖矿_以太坊2.0即将上线 现在正是挖矿红利期 普通人如何参与挖矿?...
  13. 内网远程控制安卓设备软件推荐
  14. 计算机接口cad图,(有全套CAD图纸)USB接口插件弯曲模具设计.rar
  15. HTC one/M7电信802d 毒蛇ViperOne2.1.0/高级毒蛇工具/完美root,精简/更多自定义,稳定,流畅ROM
  16. Linux的命令回收站在哪,Linux命令行删除文件到回收站
  17. 产品推广都有哪些方法可以用?
  18. Unity中自制Animation+播放完毕相应事件
  19. python查看bit_python 实现12bit灰度图像映射到8bit显示的方法
  20. Dubbo后台管理和监控中心部署

热门文章

  1. mysql oracle sql区别吗_mysql数据库的SQL语句和oracle的有什么区别?详细点
  2. mysql char null_关于mysql设置varchar 字段的默认值''和null的区别,以及varchar和char的区别...
  3. python最大堆_用Python实现最大堆
  4. python收取wss数据_Python金融应用之提取交易日+合并截面数据
  5. 阿里云物联网生活平台简介
  6. 简易TCP客户端和服务端的实现
  7. python私有方法应用场景_Python私有属性私有方法应用实例解析
  8. web应用如何确定能同时允许多少用户连接?_Web测试环境搭建+测试要点汇总
  9. python统计单词出现次数_用python统计单词出现频率
  10. mysql表分区列_如何基于char列对MySQL表进行分区?