真实对一条混合了便宜term条件和昂贵开头通配符条件的bool查询用profile API拆解,精确定位出耗时94%以上集中在通配符子节点上,展示Profile API把模糊耗时转化为精确诊断报告的真实价值。
代码真实索引50000条文档,code是随机10位字母字符串,status是active/inactive POST /prof_test/_search {"size":5,"query":{"bool":{"filter":[ {"term":{"status":"active"}}, {"wildcard":{"code":"*abc*"}} ]}}}
took=28ms, 25ms, 2ms
这组真实耗时数字本身没有太大诊断价值:查询里真实有两个filter条件,一个是便宜的term精确匹配,一个是本系列第36篇验证过真实偏慢的开头通配符wildcard查询,仅凭总耗时真实没法知道这几十毫秒到底是哪个条件造成的,也没法知道优化哪一个条件真实更有效。
代码POST /prof_test/_search {"size":5,"profile":true,"query":{"bool":{"filter":[ {"term":{"status":"active"}}, {"wildcard":{"code":"*abc*"}} ]}}}
BooleanQuery总耗时: 3200456 纳秒 (约3.2ms) ├─ TermQuery(status:active): 93583 纳秒 (约0.09ms,占比不到3%) └─ MultiTermQueryConstantScoreBlendedWrapper(code:*abc*): 3003456 纳秒 (约3.0ms,占比超过94%)
每一步都是本文上面在本机真实的Elasticsearch 8.15.0上跑出来的原始结果。
profile API 与 bool.filter 查询