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

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官方给出的解释是:

ResultSet

By 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

时间: 2024-08-08 09:39:23

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

针对MySQL大表优化方案

详解MySQL大表优化方案 (1).字段 (2).索引 (3).规范查询SQL (4).存储引擎 (5).mysql配置参数优化 (6).mysql读写分离 (7).分区和分表 单表优化: 当单表的数据不是一直在暴增,不建议使用拆分,拆分会带来逻辑,部署,运维的各种复杂度,一般以整型值为主的表在千万级以下,字符串为主的表在五百万以下是没有太大问题的.而事实上很多时候MySQL单表的性能依然有不少优化空间,甚至能正常支撑千万级以上的数据量 (1).字段 l 尽量使用TINYINT.SMALLINT

MySQL大表优化方案总结

今天看了一篇mysql大表优化方案的文章( https://mp.weixin.qq.com/s/qM6MAd_ZcrHEapz0D4nSrA ),应该说是属于科普级别的,但是技术肯定是要先大概理解了才能再深入的,深入的话推荐看 MySQL技术内幕:InnoDB存储引擎(第2版) 总结一下大表的优化方案就是: 分库分表加分区 各个层级加缓存(mysql层是缓存调参) 字段索引要优化 SQL语句别复杂 读写分离大法好 升级硬件是王道

MySQL大表备份的简单方法

MySQL大表备份是一个我们常见的问题,下面就为您介绍一个MySQL大表备份的简单方法,希望对您学习MySQL大表备份方面能有所帮助. 这里所说的大表是超过4G以上的表,我目前见到过最大为60多G的单表,对于这种表每天一个全备可以说是一件很痛苦的事.那么有没有办法,可以实现一个全备加增量的备份呢.答案当然是有的. 在常规环境直可以用全备加binlog一同保存.这种环境大多可以用一个Slave上进行备份操作. 思路:先停止Slave的同步,刷新buffer,对于Innodb 如果想直接拷贝还需要把

关于MySQL InnoDB表的二级索引是否加入主键列的问题解释

关于MySQL InnoDB表的二级索引是否加入主键,总结如下: 1对于MySQL InnoDB表的二级索引是否加入主键,官方也有明确的说明,建议线上MySQL的二级索引创建时强制加入主键所有的列,可以做到所有的MySQL 版本统一. 2.MySQL 5.6.9之前,InnoDB引擎层是会对二级索引做自动扩展,但是优化器不能识别出扩展的主键. 3.MySQL 5.6.9开始InnoDB引擎层是会对二级索引做自动扩展,优化器能识别出扩展的主键. 4.索引的大小一样,二级索引有没有加入主键列,在In

MySQL数据库表空间及索引的查看

本文我们介绍MySQL数据库表空间和索引的查看方法,并详细地给出了其代码,接下来我们一一介绍. 1.查看索引 (1)单位是GB SELECT CONCAT(ROUND(SUM(index_length)/(1024*1024*1024), 2), ' GB') AS 'Total Index Size' +------------------+ | Total Index Size | +------------------+ | 1.70 GB | +------------------+ (

全文索引----solr服务器更新全量索引

solr索引设置完成后,需要根据数据库的变化及时更新索引,索引的更新有两种方式,全量更新和增量更新.顾名思义,全量更新即删除solr服务器上的所有索引,然后重新导入数据,增量索引即只更新修改的数据,本篇文章介绍全量索引更新. 一 配置数据源 1.1 数据库 我们使用单表作为测试数据源,包括三个字段,id,title,content,方便测试,使用varchar作为主键数据类型.结构如下: 1.2 配置data-config.xml 数据源配置内容如下: <pre name="code&qu

【第一个项目】MySQL第二天早上第一次连接超时报错,解决方法com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:

MySQL第二天早上第一次连接超时报错,解决方法com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure Last packet sent to the server was 0 ms ago 最近碰到一个mysql5

Mysql:Forcing close of thread xxx user: &#39;root&#39; 的解决方法

MySQL server在中午的时候忽然挂掉.重启mysql也尽是失败,只有重启电脑才能解决,然而重装了MySQL也是不行,晚上还是挂, 去看mysql的errorlog,只能看到类似如下的信息: Forcing close of thread xxxxx user: 'root' 百度之后 发现这算属MySQL的一个bug,不管连接是通过hosts还是ip的方式,MySQL都会对DNS做反查,IP到DNS,由于反查的接续速度过慢(不管是不是isp提供的dns服务器的问题或者其他原因),大量的查

golang github.com/go-sql-driver/mysql 遇到的数据库,设置库设计不合理的解决方法

golang github.com/go-sql-driver/mysql 遇到的数据库,设置库设计不合理的解决方法,查询中报了以下这个错 Scan error on column index 2: unsupported Scan, storing driver.Value type <nil> 解决方案就是动态的把数据 字段前加一上一个COALESCE SELECT u.id,ta.`title` as `活动名` ,COALESCE(IFNULL(i.name,i.nickname) F