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.
在php循环中执行多个单个查询是否更有效,或者将所有内容保存在数组中并构建一个更大的查询 查询弹性? p>
选项1: p>
foreach($ rowset as $ row){
//执行查询:
“query”:{
“bool”:{
“must”:[
{“term”:{“a”:“'。$ row ['a']。'”}}},
{ “term”:{“b”:“'。$ row ['b']。'}}}
]
}
}
}
code> pre>
选项2: p>
$ search =“”;
foreach($ rowset as $ key => $ row){
if($ key> 0){
$ search。=',';
}
$ search。='“must”:[
{“term”:{“a”: “'。$ row ['a']。'”}},
{“term”:{“b”:“'。$ row ['b']。'”}}
]'
}
“查询”:{
“bool”:{
“should”:[
“must”:[
'。$ search。'
]
]
}
}
code> pre> \ n
在这个最小的例子中,语法可能不正确,但我希望这个想法变得清晰。 我希望选项2更快。 我没有测试它,因为我现在有多个嵌套循环,并且在我重写代码之前需要一个optinion。 p>
div>
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.
Single request using filter is the best option: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html