Mongodb Mongo-查询与索引不匹配
我正在尝试正确设置集合索引。我有一个这样的收藏:Mongodb Mongo-查询与索引不匹配,mongodb,indexing,compound-index,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, p
// 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
]
}
}
正在使用您的索引。您应该查找
winningPlan
和executionStats
部分。以下是我得到的:
"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]"
]
}
}
您可以看到有索引扫描。您的索引正在使用中。您应该查找
winningPlan
和executionStats
部分。以下是我得到的:
"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]"
]
}
}
你可以看到有一个索引扫描。那是什么语言?当一个集合中没有太多记录时,进行完整表扫描而不是索引查找也比较便宜。那是什么语言?当一个集合中没有太多记录时,进行完整表扫描而不是索引查找也比较便宜。我添加了我得到的获胜计划。这不完全相同,但表明它使用了索引,对吗?是的,输入阶段显式声明“IXSCAN”。如果索引没有被使用,它会说“COLLSCAN”非常感谢。我添加了我得到的获胜计划。这不完全相同,但表明它使用了索引,对吗?是的,输入阶段显式声明“IXSCAN”。如果没有使用索引,它会说“COLLSCAN”