dongquechan4414
2018-07-12 13:22
浏览 98
已采纳

Elasticsearch:多个小查询与一个更大的查询 - 效率?

Is it more efficient to do multiple single queries in a php loop or to keep everything in an array and build one bigger query for elastic?

Option 1:

foreach ($rowset as $row) { 
//execute query:
"query": {
                     "bool" : {
                          "must" : [
                            {"term" : { "a" : "'.$row['a'].'" }},
                            {"term" : { "b" : "'.$row['b'].'" }}
                          ]
                    }
                }

}

Option 2:

$search = "";

        foreach ($rowset as $key => $row) {

            if($key > 0) {

                $search .= ',';
            }

            $search .= '"must" : [
                            {"term" : { "a" : "'.$row['a'].'" }},
                            {"term" : { "b" : "'.$row['b'].'" }}
                          ]'
        }


"query": {
                     "bool" : {
                         "should" : [
                              "must" : [
                                '.$search.'
                              ]
                         ]
                    }
                }

The syntax may be incorrect in this minimal example but I hope the idea becomes clear. I would expect Option 2 to be faster. I didn't test it yet since I have multiple nested loops right now and wanted an optinion before I rewrite my code.

图片转代码服务由CSDN问答提供 功能建议

在php循环中执行多个单个查询是否更有效,或者将所有内容保存在数组中并构建一个更大的查询 查询弹性?

选项1:

  foreach($ rowset as $ row){
 //执行查询:
  “query”:{
“bool”:{
“must”:[
 {“term”:{“a”:“'。$ row ['a']。'”}}},
 {  “term”:{“b”:“'。$ row ['b']。'}}} 
] 
} 
} 
 
} 
   
  
 

选项2:

  $ search =“”; 
 
 foreach($ rowset as $ key => $ row){
 
  if($ key> 0){
 
 $ search。=','; 
} 
 
 $ search。='“must”:[
 {“term”:{“a”:  “'。$ row ['a']。'”}},
 {“term”:{“b”:“'。$ row ['b']。'”}} 
]'
}  
 
 
“查询”:{
  “bool”:{
“should”:[
“must”:[
'。$ search。'
] 
] 
} 
} 
   \  n 
 

在这个最小的例子中,语法可能不正确,但我希望这个想法变得清晰。 我希望选项2更快。 我没有测试它,因为我现在有多个嵌套循环,并且在我重写代码之前需要一个optinion。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doupaoshu8334 2018-07-12 13:28
    已采纳

    External resources requests are always slow. So it's better to create single request. External DB will process it with same speed, but you will save requests time.

    In the other hand you can get such big query, that your client (e.g. browser) will timeout or you can't put everything to memory for processing.


    Use single query with reasonable amount of data.

    点赞 打赏 评论
  • douyu0852 2018-07-12 13:34
    点赞 打赏 评论

相关推荐 更多相似问题