MongoDB查询在使用$ne查找非空数组的连接中速度较慢

5lwkijsr  于 2022-11-03  发布在  Go
关注(0)|答案(1)|浏览(145)

我是Mongo的新手,在匹配管道中使用$ne时,查询速度很慢(只获取匹配的记录,而不是数组为空的所有记录)
查询如下:

db.EN.aggregate([
 {
     $lookup: {
  from: 'csv_import',
  let: {pn:'$ICECAT-interface.Product.@Prod_id'},
  pipeline: [{
      $match: {
          $expr: {
              $eq: ["$$pn","$part_no"]
          }
      }
  }],
  as: 'part_number_info'
     }
 }, { $match: { part_number_info: { $ne: [] } } }
 ]).pretty();

当我删除{ $match: { part_number_info: { $ne: [] } } }时,查询在21秒内执行,而使用$ne子句执行时几乎需要2个小时。
在ICECAT-interface.Product.@Prod_id上已经有一个索引,下面是两个集合结构示例:
csv_导入:

{
  "_id": "ObjectId(\"6348339cc6e5c8ce0b7da5a4\")",
  "index": 23679,
  "product_id": 4019734,
  "part_no": "CP-HAR-EP-ADVANCED-REN-1Y",
  "vendor_standard": "Check Point"
}

中文版:

[{
  "_id": "1414",
  "ICECAT-interface": {
    "@xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
    "@xsi:noNamespaceSchemaLocation": "https://data.icecat.biz/xsd/ICECAT-interface_response.xsd",
    "Product": {
      "@Code": "1",
      "@HighPic": "https://images.icecat.biz/img/norm/high/1414-HP.jpg",
      "@HighPicHeight": "400",
      "@HighPicSize": "43288",
      "@HighPicWidth": "400",
      "@ID": "1414",
      "@LowPic": "https://images.icecat.biz/img/norm/low/1414-HP.jpg",
      "@LowPicHeight": "200",
      "@LowPicSize": "17390",
      "@LowPicWidth": "200",
      "@Name": "C6614NE",
      "@IntName": "C6614NE",
      "@LocalName": "",
      "@Pic500x500": "https://images.icecat.biz/img/gallery_mediums/img_1414_medium_1480667779_072_2323.jpg",
      "@Pic500x500Height": "500",
      "@Pic500x500Size": "101045",
      "@Pic500x500Width": "500",
      "@Prod_id": "C6614NE",

解决方案

我确实在csv_import中的part_no字段上添加了索引,并将查询顺序从小到大更改(EN为27 GB,csv_import为几MB)
最终查询:(包括尼姆罗德serok提出的建议)

db.csv_import.aggregate([
 {
     $lookup: {
  from: 'EN',
  let: {pn:'$part_no'},
  pipeline: [{
      $match: {
          $expr: {
              $eq: ["$$pn","$ICECAT-interface.Product.@Prod_id"]
          }
      }
  }],
  as: 'part_number_info'
     }
 },{$match: {"part_number_info.0": {$exists: true}}}
 ])
gzszwxb4

gzszwxb41#

更好的选择是用途:

{$match: {"part_number_info.0": {$exists: true}}}

了解它在playground example上的工作原理

相关问题