Elasticsearch性能优化 #
一、优化概述 #
1.1 优化维度 #
text
优化维度
├── 硬件优化
│ ├── CPU
│ ├── 内存
│ └── 存储
├── 配置优化
│ ├── JVM
│ └── 系统参数
├── 索引优化
│ ├── 映射设计
│ └── 分片策略
└── 查询优化
├── 查询语法
└── 缓存策略
二、硬件优化 #
2.1 CPU优化 #
text
CPU建议
├── 核心数
│ └── 8-64核
├── 主频
│ └── 越高越好
├── 使用场景
│ ├── 写入密集:更多核心
│ └── 查询密集:更高主频
└── 避免超线程
└── 关闭或谨慎使用
2.2 内存优化 #
text
内存建议
├── 堆内存
│ ├── 最大32GB
│ ├── 不超过物理内存50%
│ └── Xms=Xmx
├── 系统内存
│ └── 留给Lucene缓存
└── 内存锁定
└── bootstrap.memory_lock: true
2.3 存储优化 #
text
存储建议
├── 类型
│ ├── SSD(推荐)
│ └── NVMe(最佳)
├── IOPS
│ └── 越高越好
├── 避免使用
│ ├── NAS
│ └── 网络存储
└── RAID
└── RAID0(性能)或RAID10(安全)
三、JVM优化 #
3.1 堆内存配置 #
text
-Xms16g
-Xmx16g
3.2 GC配置 #
text
-XX:+UseG1GC
-XX:G1HeapRegionSize=32m
-XX:InitiatingHeapOccupancyPercent=30
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=/var/log/elasticsearch/heapdump.hprof
3.3 其他JVM参数 #
text
-XX:+AlwaysPreTouch
-XX:+UseCompressedOops
-XX:+UseCompressedClassPointers
-Djava.io.tmpdir=/tmp
-XX:+ExitOnOutOfMemoryError
四、系统参数优化 #
4.1 文件描述符 #
bash
ulimit -n 65536
在 /etc/security/limits.conf 添加:
text
elasticsearch soft nofile 65536
elasticsearch hard nofile 65536
4.2 虚拟内存 #
bash
sysctl -w vm.max_map_count=262144
在 /etc/sysctl.conf 添加:
text
vm.max_map_count=262144
4.3 关闭Swap #
bash
swapoff -a
或在 /etc/fstab 注释swap行。
4.4 禁用THP #
bash
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag
五、索引优化 #
5.1 映射优化 #
bash
PUT /products
{
"mappings": {
"properties": {
"title": {
"type": "text",
"norms": false,
"index_options": "freqs"
},
"status": {
"type": "keyword",
"doc_values": true
},
"price": {
"type": "scaled_float",
"scaling_factor": 100
}
}
}
}
优化选项:
| 选项 | 说明 |
|---|---|
| norms: false | 不需要评分时禁用 |
| index_options | 减少索引信息 |
| doc_values: false | 不需要排序聚合时禁用 |
| enabled: false | 不索引字段 |
5.2 分片优化 #
text
分片优化
├── 主分片数量
│ ├── 单分片10-50GB
│ └── 考虑扩展性
├── 副本数量
│ ├── 生产至少1个
│ └── 根据查询负载调整
└── 分片分布
└── 均衡分布到节点
5.3 批量索引优化 #
bash
PUT /products/_settings
{
"index": {
"refresh_interval": "-1",
"number_of_replicas": 0
}
}
索引完成后恢复:
bash
PUT /products/_settings
{
"index": {
"refresh_interval": "1s",
"number_of_replicas": 1
}
}
POST /products/_forcemerge?max_num_segments=1
六、查询优化 #
6.1 使用filter #
bash
GET /products/_search
{
"query": {
"bool": {
"must": [
{ "match": { "name": "iPhone" } }
],
"filter": [
{ "term": { "brand": "Apple" } },
{ "range": { "price": { "lte": 1000 } } }
]
}
}
}
6.2 避免深度分页 #
bash
GET /products/_search
{
"size": 10,
"query": { "match_all": {} },
"sort": [
{ "created_at": "asc" },
{ "_id": "asc" }
],
"search_after": ["2024-01-01", "abc123"]
}
6.3 字段过滤 #
bash
GET /products/_search
{
"query": { "match_all": {} },
"_source": ["name", "price"]
}
6.4 使用路由 #
bash
GET /products/_search?routing=apple
{
"query": {
"match": { "name": "iPhone" }
}
}
6.5 预热查询 #
bash
POST /products/_cache/clear
POST /products/_forcemerge?max_num_segments=1
七、缓存优化 #
7.1 查询缓存 #
bash
PUT /products/_settings
{
"index.queries.cache.enabled": true,
"index.queries.cache.size": "10%"
}
7.2 字段数据缓存 #
bash
PUT /_cluster/settings
{
"persistent": {
"indices.fielddata.cache.size": "40%"
}
}
7.3 请求缓存 #
bash
PUT /products/_settings
{
"index.requests.cache.enable": true
}
八、写入优化 #
8.1 批量写入 #
bash
POST /_bulk
{"index": {"_index": "products", "_id": "1"}}
{"name": "iPhone 15", "price": 999}
8.2 调整刷新间隔 #
bash
PUT /products/_settings
{
"index": {
"refresh_interval": "30s"
}
}
8.3 调整translog #
bash
PUT /products/_settings
{
"index": {
"translog": {
"durability": "async",
"sync_interval": "30s",
"flush_threshold_size": "1gb"
}
}
}
九、监控与调优 #
9.1 性能监控 #
bash
GET /_nodes/stats
GET /_nodes/hot_threads
GET /_cat/indices?v&health=yellow
9.2 慢查询日志 #
bash
PUT /products/_settings
{
"index.search.slowlog.threshold.query.warn": "10s",
"index.search.slowlog.threshold.query.info": "5s",
"index.search.slowlog.threshold.query.debug": "2s",
"index.search.slowlog.threshold.fetch.warn": "1s"
}
9.3 慢索引日志 #
bash
PUT /products/_settings
{
"index.indexing.slowlog.threshold.index.warn": "10s",
"index.indexing.slowlog.threshold.index.info": "5s"
}
十、最佳实践总结 #
10.1 优化优先级 #
text
优化优先级
├── 1. 硬件优化
│ └── SSD、足够内存
├── 2. 索引设计
│ └── 合理映射和分片
├── 3. 查询优化
│ └── 使用filter、避免深度分页
├── 4. 配置优化
│ └── JVM、系统参数
└── 5. 缓存优化
└── 合理使用缓存
10.2 性能测试 #
text
性能测试建议
├── 基准测试
│ └── 建立性能基线
├── 压力测试
│ └── 测试极限性能
├── 对比测试
│ └── 优化前后对比
└── 持续监控
└── 监控性能变化
十一、总结 #
本章介绍了Elasticsearch性能优化:
- 硬件选择影响性能基础
- JVM配置影响内存使用
- 索引设计影响存储和查询效率
- 查询优化提升响应速度
- 缓存减少重复计算
- 持续监控发现性能问题
下一步,我们将学习监控告警。
最后更新:2026-03-27