Elasticsearch 调优完全手册:从 JVM 配置到索引设计,查询速度提升 5 倍!🔍

本文阅读 1 分钟
首页 技术分享 正文

二、Elasticsearch 生产级调优

2.1 JVM 堆内存黄金法则

# jvm.options 配置原则
-Xms31g
-Xmx31g  # 不超过物理内存50%
-XX:+UseG1GC
-XX:G1ReservePercent=25
-XX:InitiatingHeapOccupancyPercent=30

# 重要提示:
# 1. 堆内存超过32GB会禁用压缩指针
# 2. 预留50%内存给Lucene文件缓存

2.2 索引设计规范

分片计算公式:

总分片数 = 数据总量(GB) / 单个分片推荐大小(30-50GB)

模版示例:

PUT _template/prod_template
{
  "index_patterns": ["prod-*"],
  "settings": {
    "number_of_shards": 5,
    "number_of_replicas": 1,
    "refresh_interval": "30s",
    "translog.durability": "async",
    "mapping": {
      "total_fields": {
        "limit": 1000
      }
    }
  }
}

2.3 查询性能优化

DSL优化技巧:

GET /orders/_search
{
  "query": {
    "bool": {
      "filter": [  // 使用filter避免算分
        {"range": {"create_time": {"gte": "now-30d/d"}}},
        {"term": {"status": "completed"}}
      ],
      "must": [
        {"match": {"product_name": "手机"}}
      ]
    }
  },
  "aggs": {
    "sales": {
      "date_histogram": {
        "field": "create_time",
        "calendar_interval": "day",
        "min_doc_count": 0
      }
    }
  },
  "size": 0  // 不返回原始文档
}

本文来自投稿,不代表本站立场,如若转载,请注明出处:
-- 展开阅读全文 --
PHP优化终极指南:20个技巧让网站速度提升300% 🚀 | 开发者必看
« 上一篇 03-29
PostgreSQL 数据库优化终极指南:从参数调优到 SQL 加速,TPS 提升 400%!📊
下一篇 » 03-29