參考文章:
1. 將 ELASTICSEARCH 寫入速度優化到極限
2. Mastering Elasticsearch(中文版)
3. How to Maximize Elasticsearch Indexing Performance (Part 1)
4. How to Maximize Elasticsearch Indexing Performance (Part 2)
5. How to Maximize Elasticsearch Indexing Performance (Part 3)
背景:
- ES讀取文件入庫速度比其它系統寫文件速度慢,導致文件大量堆積
- ES入庫方式採取:python調用ElasticSearch的bulk接口實現數據的批量插入
嘗試的優化方案
方案一
創建更多的python子進程去讀取文件
方案二
將讀寫的文件目錄掛載內存文件系統
方案三
修改ElasticSearch的配置文件ElasticSearch.yml(Elasticsearch的配置文件在/etc/elasticsearch/)
1. 導入大量數據 index.refresh_interval: 30
2. 去掉副本 index.number_of_replicas: 0
3. 機械硬盤併發訪問問題index.merge.scheduler.max_thread_count: 1
4. 如果系統可以接受一定機率的數據丟失,調整 translog 持久化策略爲週期性和一定大小的時候 flush:
* index.translog.durability: async
* index.translog.sync_interval: 120s
* index.translog.flush_threshold_size: 1024mb
* index.translog.flush_threshold_period: 120m
方案四
修改ElasticSearch.yml配置文件
bootstrap.mlockall:true
設置爲true來鎖住內存。
因爲當jvm開始swapping時es的效率會降低,所以要保證它不swap。
[root@timeserver ~]# vim /etc/sysctl.conf
# Controls the maximum number of shared memory segments, in pages
kernel.shmall = 4294967296
vm.swappiness=10
[root@timeserver ~]# sysctl -p
方案五
用性能更好的硬件
用ssd替換機械硬盤,可取消ElasticSearch.yml裏關於機械硬盤併發訪問的修改
關於cpu有大佬測試數據如下:
2650的 CPU 上壓了一下寫入性能,2.5k,70個字段的 doc 可以跑到4.5w/s
相同的數據,2430只能跑到1.3w/s
方案六
文件屬性特徵的影響
對於每個字段如果都使用分詞插件去做語義分析,也會大量消耗cpu