ElasticSearch插入數據優化

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}}'
發佈了182 篇原創文章 · 獲贊 106 · 訪問量 60萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章