ES慢查询优化方案 profile API

4,467 阅读2分钟

Profile API

Profile API 用于定位查询过程中的异常耗时问题的。可以通过在 query 部分上方提供 “profile: true” 来启用Profile API。

GET /_search
{
  "profile": true,
  "query" : {
    "match" : { "message" : "message number" }
  }
}

profile接口返回信息如下:

{
   "profile": {
        "shards": [
           {
              "id": "[2aE02wS1R8q_QFnYu6vDVQ][twitter][1]",  
              "searches": [
                 {
                    "query": [...],             
                    "rewrite_time": 51443,      
                    "collector": [...]          
                 }
              ],
              "aggregations": [...]             
           }
        ]
     }
}

返回包含分片id,搜索过程信息searches和聚合信息aggregations。

searches里还有另外三个元素:

  1. query
  2. rewrite_time
  3. collecotor

aggregations包含了聚合过程中具体执行信息。

元素含义

Query

          "query": [
                    {
                       "type": "BooleanQuery",
                       "description": "message:message message:number",
                       "time": "1.873811000ms",
                       "breakdown": {
                          "score": 51306,
                          "score_count": 4,
                          "build_scorer": 2935582,
                          "build_scorer_count": 1,
                          "match": 0,
                          "match_count": 0,
                          "create_weight": 919297,
                          "create_weight_count": 1,
                          "next_doc": 53876,
                          "next_doc_count": 5,
                          "advance": 0,
                          "advance_count": 0
                       },
                       "children": [
                          {
                             "type": "TermQuery",
                             "description": "message:message",
                             "time": "0.3919430000ms",
                             "breakdown": {
                                "score": 28776,
                                "score_count": 4,
                                "build_scorer": 784451,
                                "build_scorer_count": 1,
                                "match": 0,
                                "match_count": 0,
                                "create_weight": 1669564,
                                "create_weight_count": 1,
                                "next_doc": 10111,
                                "next_doc_count": 5,
                                "advance": 0,
                                "advance_count": 0
                             }
                          },
                          {
                             "type": "TermQuery",
                             "description": "message:number",
                             "time": "0.2106820000ms",
                             "breakdown": {
                                "score": 4552,
                                "score_count": 4,
                                "build_scorer": 42602,
                                "build_scorer_count": 1,
                                "match": 0,
                                "match_count": 0,
                                "create_weight": 89323,
                                "create_weight_count": 1,
                                "next_doc": 2852,
                                "next_doc_count": 5,
                                "advance": 0,
                                "advance_count": 0
                             }
                          }
                       ]
                    }
                 ]
  1. type:显示的是哪种类型的查询被触发,常见的有BooleanQuery,TermQuery等
  2. time:执行该查询所用的时间
  3. breakdown:查询的更详细细节,主要与lucene参数有关
  4. chidren:具有多个关键字的查询被拆成相应术语的布尔查询

Rewrite Time

"rewrite_time": 51443

由于多个关键字会分解以创建个别查询,所以在这个过程中肯定会花费一些时间。将查询重写一个或多个组合查询的时间被称为“重写时间”。

Collectors

     "collector": [
                    {
                       "name": "CancellableCollector",
                       "reason": "search_cancelled",
                       "time": "0.3043110000ms",
                       "children": [
                         {
                           "name": "SimpleTopScoreDocCollector",
                           "reason": "search_top_hits",
                           "time": "0.03227300000ms"
                         }
                       ]
                    }
                 ]

在Lucene中,收集器是负责收集原始结果,收集和组合结果,执行结果排序等的过程。

Kibana可视化profile分析

除了使用profile API外,使用kibana可视化profile界面更加直观。

image.png

界面分为query profile和Aggregation profile部分,并详细列出了请求过程的耗时和占比,更加直观,推荐使用!