从芯片分析师到MLPerf联合主席:AI基准测试的实践与思考
2026/5/12 10:52:30
大慢查询隔离是 Elasticsearch 中一种资源管理和性能保护机制,主要目的是防止大型、耗时的查询拖垮整个集群性能,特别是保障核心业务查询的响应时间。
主要通过两种方式实现隔离:
// 线程池配置thread_pool:search:size:线程数 queue_size:队列长度7.x+版本默认已优化。GET/_search{"timeout":"30s",// 超时时间"terminate_after":10000,// 最大返回文档数"track_total_hits":10000,// 限制总命中数计算"query":{...},"aggs":{"large_agg":{"composite":{"size":1000// 限制聚合桶数量}}}}PUT/my-index/_settings{"index.max_result_window":10000,// 最大 from+size"index.max_inner_result_window":100,// 内部命中最大数"index.max_docvalue_fields_search":100// docvalue字段数限制}# elasticsearch.ymlsearch.max_buckets:65536# 聚合桶数上限indices.query.bool.max_clause_count:1024# bool查询子句数上限# 断路器设置indices.breaker.total.limit:70%# 总内存断路器indices.breaker.request.limit:60%# 单个请求内存限制indices.breaker.fielddata.limit:40%# fielddata内存限制// 通过查询类型标识GET/_search{"pre_filter_shard_size":128,// 预过滤分片数"request_cache":true,// 对可缓存的查询启用缓存// 对于已知的大查询"search_type":"query_then_fetch",// 避免DFS导致的全局计算"batched_reduce_size":512// 分批归并结果}// 使用安全插件限制不同用户的查询能力PUT/_security/role/read_only_role{"indices":[{"names":["logs-*"],"privileges":["read"],"query":{"bool":{"must_not":[{"exists":{"field":"secret_field"}}]}},"field_security":{"grant":["public_*","timestamp"]},"allow_restricted_indices":false}],"cluster":["monitor"],"applications":[],"run_as":[],"metadata":{},"transient_metadata":{"enabled":true}}// 启用慢查询日志PUT/_cluster/settings{"transient":{"logger.org.elasticsearch.search.slowlog":"DEBUG","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.query.trace":"500ms"}}# 查看查询队列状态GET /_cat/thread_pool/search?v&h=name,active,queue,rejected# 查看正在运行的查询GET /_tasks?actions=*search&detailed# 节点状态GET /_nodes/stats/thread_poolquery_string替代部分script查询docvalue_fields替代_source检索force_merge减少分段数// 使用 Painless 脚本限制复杂查询{"script_fields":{"dangerous_field":{"script":{"source":"""if(ctx._source.some_field.length>10000){thrownewException("Field too large");}returnctx._source.some_field;""","lang":"painless"}}}}*查询大慢查询隔离是 Elasticsearch 运维中的关键策略,能有效防止 “一个慢查询拖垮整个集群” 的雪崩效应,保障集群的稳定性和核心业务的查询性能。