Mongo - 查询不匹配索引

时间:2017-01-19 03:15:54

标签: mongodb indexing compound-index

我正在尝试正确设置我的集合索引。我有一个类似的集合:

// videos collection
{
  _id: idString,
  ts: date,
  owner_id: idString,
  published: boolean,
  readyToPlay: boolean,
  private: boolean,
  deleted: boolean,
}

我将其索引为:

videos.ensureIndex({
  owner_id: 1,
  deleted: 1,

  published: 1,
  readyToPlay: 1,
  private: 1,

  ts: -1,
})

我正在查询它:

videos.find({
  owner_id: { $in: ids },
  deleted: false,
  published: true,
  private: false,
  readyToPlay: true,
})

当我解释查询时,我得到:

[ {
  queryPlanner: { 
    plannerVersion: 1,
    namespace: 'videos',
    indexFilterSet: false,
    parsedQuery: { '$and': [
      { isPrivate: { '$eq': false } },
      { published: { '$eq': true } },
      { readyToPlay: { '$eq': true } },
      { owner_id: { '$in': [...] } },
      { deleted: { '$eq': false } },
    ] },
    winningPlan: [...],
    rejectedPlans: [],
  },
  executionStats: {
    nReturned: some docs,
    totalDocsExamined: all the docs,
    ...
  },
  serverInfo: { ... },
]

由于indexFilterSet为false,似乎我的索引无法正常工作。我该怎么改变它?

获奖计划是:

{ stage: 'FETCH',
  inputStage: {
    stage: 'SORT_MERGE',
    sortPattern: { ts: 1 },
    inputStages: [{
      stage: 'IXSCAN',
      keyPattern: {
        owner_id: 1,
        deleted: 1,
        published: 1,
        readyToPlay: 1,
        isPrivate: 1,
        ts: -1
      },
      indexName: 'owner_id_1_deleted_1_published_1_readyToPlay_1_isPrivate_1_ts_-1',
      isMultiKey: false,
      isUnique: false,
      isSparse: false,
      isPartial: false,
      indexVersion: 1,
      direction: 'backward',
      indexBounds: { owner_id: [ '["id1", "id1"]' ],
        deleted: [ '[false, false]' ],
        published: [ '[true, true]' ],
        readyToPlay: [ '[true, true]' ],
        isPrivate: [ '[false, false]' ],
        ts: [ '[MinKey, MaxKey]' ]
      }
    },
    // a lot more stages, one for each key in ids
    ]
  }
}

1 个答案:

答案 0 :(得分:1)

您的索引正在使用中。您应该查找winningPlanexecutionStats部分。这就是我得到的:

"winningPlan" : {
    "stage" : "FETCH",
    "inputStage" : {
            "stage" : "IXSCAN",
            "keyPattern" : {
                    "owner_id" : 1,
                    "deleted" : 1,
                    "published" : 1,
                    "readyToPlay" : 1,
                    "private" : 1,
                    "ts" : -1
            },
            "indexName" : "owner_id_1_deleted_1_published_1_readyToPlay_1_private_1_ts_-1",
            "isMultiKey" : false,
            "isUnique" : false,
            "isSparse" : false,
            "isPartial" : false,
            "indexVersion" : 1,
            "direction" : "forward",
            "indexBounds" : {
                    "owner_id" : [
                            "[\"1\", \"1\"]"
                    ],
                    "deleted" : [
                            "[false, false]"
                    ],
                    "published" : [
                            "[true, true]"
                    ],
                    "readyToPlay" : [
                            "[true, true]"
                    ],
                    "private" : [
                            "[true, true]"
                    ],
                    "ts" : [
                            "[MaxKey, MinKey]"
                    ]
            }
    }

您可以看到有索引扫描。