MongoDB查询不使用交集索引

时间:2016-08-25 18:16:08

标签: mongodb

我的MongoDB 3.28包含一个集合,并且在不同的字段上有很少的索引。我正在执行一个包含许多过滤器的查询,期望intersection indexes启动,但由于某种原因,它不是。此外,它未显示在explain(true) allPlans

查询

{
  "$and": [
    {
      "category": {
        "$in": [
          1,
          5
        ]
      }
    },
    {
      "city": {
        "$in": [
          "y"
        ]
      }
    },
    {
      "neighbourhood": {
        "$in": [
          "x",
          null
        ]
      }
    },
    {
      "$or": [
        {
          "price": {
            "$lte": 3
          }
        },
        {
          "price": null
        }
      ]
    }
  ]
}

db.items.find(查询).explain(真)

{
  "queryPlanner" : {
    "plannerVersion" : 1,
    "namespace" : "xxx.items",
    "indexFilterSet" : false,
    "parsedQuery" : {
      "$and" : [
        {
          "$or" : [
            {
              "price" : {
                "$eq" : null
              }
            },
            {
              "price" : {
                "$lte" : 3
              }
            }
          ]
        },
        {
          "category" : {
            "$in" : [
              1,
              5
            ]
          }
        },
        {
          "city" : {
            "$in" : [
              "y"
            ]
          }
        },
        {
          "neighbourhood" : {
            "$in" : [
              null,
              "x"
            ]
          }
        }
      ]
    },
    "winningPlan" : {
      "stage" : "FETCH",
      "filter" : {
        "$and" : [
          {
            "$or" : [
              {
                "price" : {
                  "$eq" : null
                }
              },
              {
                "price" : {
                  "$lte" : 3
                }
              }
            ]
          },
          {
            "category" : {
              "$in" : [
                1,
                5
              ]
            }
          },
          {
            "neighbourhood" : {
              "$in" : [
                null,
                "x"
              ]
            }
          }
        ]
      },
      "inputStage" : {
        "stage" : "IXSCAN",
        "keyPattern" : {
          "city" : 1,
          "updatedAt" : 1
        },
        "indexName" : "city_updatedAt",
        "isMultiKey" : false,
        "isUnique" : false,
        "isSparse" : false,
        "isPartial" : false,
        "indexVersion" : 1,
        "direction" : "forward",
        "indexBounds" : {
          "city" : [
            "[\"y\", \"y\"]"
          ],
          "updatedAt" : [
            "[MinKey, MaxKey]"
          ]
        }
      }
    },
    "rejectedPlans" : [
      {
        "stage" : "FETCH",
        "filter" : {
          "$and" : [
            {
              "category" : {
                "$in" : [
                  1,
                  5
                ]
              }
            },
            {
              "city" : {
                "$in" : [
                  "y"
                ]
              }
            },
            {
              "neighbourhood" : {
                "$in" : [
                  null,
                  "x"
                ]
              }
            }
          ]
        },
        "inputStage" : {
          "stage" : "FETCH",
          "filter" : {
            "$or" : [
              {
                "price" : {
                  "$eq" : null
                }
              },
              {
                "price" : {
                  "$lte" : 3
                }
              }
            ]
          },
          "inputStage" : {
            "stage" : "IXSCAN",
            "keyPattern" : {
              "price" : 1
            },
            "indexName" : "price_1",
            "isMultiKey" : false,
            "isUnique" : false,
            "isSparse" : false,
            "isPartial" : false,
            "indexVersion" : 1,
            "direction" : "forward",
            "indexBounds" : {
              "price" : [
                "[null, null]",
                "[-inf.0, 3.0]"
              ]
            }
          }
        }
      },
      {
        "stage" : "FETCH",
        "filter" : {
          "$and" : [
            {
              "$or" : [
                {
                  "price" : {
                    "$eq" : null
                  }
                },
                {
                  "price" : {
                    "$lte" : 3
                  }
                }
              ]
            },
            {
              "category" : {
                "$in" : [
                  1,
                  5
                ]
              }
            },
            {
              "neighbourhood" : {
                "$in" : [
                  null,
                  "x"
                ]
              }
            }
          ]
        },
        "inputStage" : {
          "stage" : "IXSCAN",
          "keyPattern" : {
            "city" : 1
          },
          "indexName" : "city_1",
          "isMultiKey" : false,
          "isUnique" : false,
          "isSparse" : false,
          "isPartial" : false,
          "indexVersion" : 1,
          "direction" : "forward",
          "indexBounds" : {
            "city" : [
              "[\"y\", \"y\"]"
            ]
          }
        }
      },
      {
        "stage" : "FETCH",
        "filter" : {
          "$and" : [
            {
              "$or" : [
                {
                  "price" : {
                    "$eq" : null
                  }
                },
                {
                  "price" : {
                    "$lte" : 3
                  }
                }
              ]
            },
            {
              "city" : {
                "$in" : [
                  "y"
                ]
              }
            },
            {
              "neighbourhood" : {
                "$in" : [
                  null,
                  "x"
                ]
              }
            }
          ]
        },
        "inputStage" : {
          "stage" : "IXSCAN",
          "keyPattern" : {
            "category" : 1,
            "updatedAt" : -1
          },
          "indexName" : "category",
          "isMultiKey" : false,
          "isUnique" : false,
          "isSparse" : false,
          "isPartial" : false,
          "indexVersion" : 1,
          "direction" : "forward",
          "indexBounds" : {
            "category" : [
              "[1.0, 1.0]",
              "[5.0, 5.0]"
            ],
            "updatedAt" : [
              "[MaxKey, MinKey]"
            ]
          }
        }
      }
    ]
  },
  "serverInfo" : {
    "host" : "xxx",
    "port" : 27017,
    "version" : "3.2.8",
    "gitVersion" : "ed70e33130c977bda0024c125b56d159573dbaf0"
  },
  "ok" : 1
}

db.items.getIndexes()

[
  {
    "v" : 1,
    "key" : {
      "_id" : 1
    },
    "name" : "_id_",
    "ns" : "xxx.items"
  },
  {
    "v" : 1,
    "unique" : true,
    "key" : {
      "RecordID" : 1
    },
    "name" : "RecordID",
    "ns" : "xxx.items"
  },
  {
    "v" : 1,
    "key" : {
      "SubCatID" : 1,
      "updatedAt" : -1
    },
    "name" : "category",
    "ns" : "xxx.items"
  },
  {
    "v" : 1,
    "key" : {
      "updatedAt" : -1
    },
    "name" : "updatedAt_-1",
    "ns" : "xxx.items",
    "background" : true
  },
  {
    "v" : 1,
    "key" : {
      "city" : 1
    },
    "name" : "city_1",
    "ns" : "xxx.items"
  },
  {
    "v" : 1,
    "key" : {
      "search" : 1
    },
    "name" : "search_1",
    "ns" : "xxx.items",
    "background" : true
  },
  {
    "v" : 1,
    "key" : {
      "rooms" : 1
    },
    "name" : "rooms_1",
    "ns" : "xxx.items",
    "background" : true
  },
  {
    "v" : 1,
    "key" : {
      "price" : 1
    },
    "name" : "price_1",
    "background" : true,
    "ns" : "xxx.items"
  },
  {
    "v" : 1,
    "key" : {
      "city" : 1,
      "updatedAt" : 1
    },
    "name" : "city_updatedAt",
    "ns" : "xxx.items",
    "background" : true
  }
]

示例记录:db.items.find()。limit(1)[0]

{
"_id" : ObjectId("568ee714578df40300ac65b0"),
"type" : "z",
"CatID" : 2,
"category" : 5,
"RecordID" : "1469882",
"title" : "x - x x",
"subtitle" : "x/x' - 7 x",
"subtitle2" : "3,700,000",
"someProps1" : false,
"someProps2" : 14,
"subtite3" : "some title",
"type" : 3,
"img" : "x",
"URL" : "y",
"someProps" : 0,
"latitude" : 31.809843,
"longitude" : 35.191562,
"Map_address" : {
"adress" : "x - x x",
"lat" : 31.809843,
"long" : 35.191562
},
"created_at" : ISODate("2016-01-07T22:30:44.425Z"),
"updated_at" : ISODate("2016-01-14T15:53:43.110Z"),
"price" : 3700000,
"rooms" : 7,
"updatedAt" : 1452292244,
"search" : "x - x x 14-01-2016",
"street" : null
}

0 个答案:

没有答案