您的位置:首页 > 其它

ElasticSearch插入数据优化

2016-09-12 08:48 260 查看
1、多线程程序插入

可以根据服务器情况开启多个线程index,速度可以提高n倍, n>=2。如在上篇博文《将Mysql数据导入到ElasticSearch集群》示例的代码,开启了10个线程。但是可以线程也不是越多越好,要根据你磁盘的io,cpu等而定。

2、设置复制分片数量

如果有多台机器,可以以每台设置n个shards的方式,根据业务情况,可以考虑取消replias(复制分片),等数据插入结束以后再进行更新操作,设置复制分片。此方法可使插入速度提高一倍。但要注意,主分片的数量是不可以更改的。

curl -XPUT 'http://xxxxx:9400/index_rfm_test/' -d '{
"settings" : {
"number_of_shards" : 20,
"number_of_replicas" : 0
}
}'

//数据插入结束,更新复制分片为1
curl -XPUT 'http://xxxxx:9400/index_rfm_test/_settings' -d '{"index":{"number_of_replicas":1}}'


3、提高ES占用内存

内存适当调大,初始是256M,最大1G, 调大后,最小和最大一样,避免GC, 并根据机器情况,设置内存大小。

$ bin/elasticsearch -f -Xmx4g -Xms4g -Des.index.storage.type=memory


原文:http://www.elasticsearch.org/guide/reference/setup/installation.html

4、减少shard刷新间隔

curl -XPUT 'http://xxxxx:9400/index_rfm_test/_settings' -d '{
"index" : {
"refresh_interval" : "-1"
}
}'

//完成插入后再修改为初始值
curl -XPUT 'http://xxxxx:9400/index_rfm_test/_settings' -d '{
"index" : {
"refresh_interval" : "1s"
}
}'


5、设置一个shard的段segment最大数

//可以减少段文件数,提高查询速度
curl -XPUT 'http://xxxxx:9400/index_rfm_test/_optimize?max_num_segments=5'

//强制merger
curl -XPOST 'http://xxxxx:9400/index_rfm_test/_forcemerge?max_num_segments=1&only_expunge_deletes=true'


注意:有时候可能需要多次执行

原文:http://www.elasticsearch.org/guide/reference/api/admin-indices-update-settings.html

原文:http://www.elasticsearch.org/guide/reference/index-modules/merge.html

6、去掉mapping中_all域

Index中默认会有_all的域,这个会给查询带来方便,但是会增加索引时间和索引尺寸

"_all" : {"enabled" : false}


原文:http://www.elasticsearch.org/guide/reference/mapping/all-field.html

7、适当增大节点threadpool参数

curl -XPUT 'http://xxxxx:9400/_cluster/settings' -d '{"transient":{"threadpool.index.queue_size":5000,"threadpool.bulk.queue_size": 5000}}'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  elasticsearch 服务器