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

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.

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘