【发布时间】:2022-09-23 05:08:56
【问题描述】:
我有这样的索引:businessId_1_accountId_1_dataHash_1_deleted_1
而像这样的查询,不使用它,而是使用另一个。我不明白为什么。
db.getCollection(\"transactions\").find({
\"businessId\": ObjectId(\"62c56bbdba7f1d001368f217\"),
\"accountId\": ObjectId(\"62c56bbef2c6530d4a9b5976\"),
\"dataHash\": \"00002478a5c9594923833b3534b2d0b17bb7298a\",
deleted: { $ne: true }
}, { _id: true }).explain();
这是解释结果
{
\"explainVersion\" : \"1\",
\"queryPlanner\" : {
\"namespace\" : \"stage.transactions\",
\"indexFilterSet\" : false,
\"parsedQuery\" : {
\"$and\" : [
{
\"accountId\" : {
\"$eq\" : ObjectId(\"62c56bbef2c6530d4a9b5976\")
}
},
{
\"businessId\" : {
\"$eq\" : ObjectId(\"62c56bbdba7f1d001368f217\")
}
},
{
\"dataHash\" : {
\"$eq\" : \"00002478a5c9594923833b3534b2d0b17bb7298a\"
}
},
{
\"deleted\" : {
\"$not\" : {
\"$eq\" : true
}
}
}
]
},
\"queryHash\" : \"29075D21\",
\"planCacheKey\" : \"7C80754C\",
\"maxIndexedOrSolutionsReached\" : false,
\"maxIndexedAndSolutionsReached\" : false,
\"maxScansToExplodeReached\" : false,
\"winningPlan\" : {
\"stage\" : \"PROJECTION_SIMPLE\",
\"transformBy\" : {
\"_id\" : true
},
\"inputStage\" : {
\"stage\" : \"FETCH\",
\"filter\" : {
\"$and\" : [
{
\"accountId\" : {
\"$eq\" : ObjectId(\"62c56bbef2c6530d4a9b5976\")
}
},
{
\"dataHash\" : {
\"$eq\" : \"00002478a5c9594923833b3534b2d0b17bb7298a\"
}
}
]
},
\"inputStage\" : {
\"stage\" : \"IXSCAN\",
\"keyPattern\" : {
\"businessId\" : 1.0,
\"reason\" : 1.0,
\"deleted\" : 1.0
},
\"indexName\" : \"businessId_1_reason_1_deleted_1\",
\"isMultiKey\" : false,
\"multiKeyPaths\" : {
\"businessId\" : [
],
\"reason\" : [
],
\"deleted\" : [
]
},
\"isUnique\" : false,
\"isSparse\" : false,
\"isPartial\" : false,
\"indexVersion\" : 2.0,
\"direction\" : \"forward\",
\"indexBounds\" : {
\"businessId\" : [
\"[ObjectId(\'62c56bbdba7f1d001368f217\'), ObjectId(\'62c56bbdba7f1d001368f217\')]\"
],
\"reason\" : [
\"[MinKey, MaxKey]\"
],
\"deleted\" : [
\"[MinKey, true)\",
\"(true, MaxKey]\"
]
}
}
}
},
\"rejectedPlans\" : [
{
\"stage\" : \"PROJECTION_SIMPLE\",
\"transformBy\" : {
\"_id\" : true
},
\"inputStage\" : {
\"stage\" : \"FETCH\",
\"filter\" : {
\"$and\" : [
{
\"accountId\" : {
\"$eq\" : ObjectId(\"62c56bbef2c6530d4a9b5976\")
}
},
{
\"dataHash\" : {
\"$eq\" : \"00002478a5c9594923833b3534b2d0b17bb7298a\"
}
}
]
},
\"inputStage\" : {
\"stage\" : \"IXSCAN\",
\"keyPattern\" : {
\"businessId\" : 1.0,
\"tags\" : 1.0,
\"deleted\" : 1.0
},
\"indexName\" : \"businessId_1_tags_1_deleted_1\",
\"isMultiKey\" : true,
\"multiKeyPaths\" : {
\"businessId\" : [
],
\"tags\" : [
\"tags\"
],
\"deleted\" : [
]
},
\"isUnique\" : false,
\"isSparse\" : false,
\"isPartial\" : false,
\"indexVersion\" : 2.0,
\"direction\" : \"forward\",
\"indexBounds\" : {
\"businessId\" : [
\"[ObjectId(\'62c56bbdba7f1d001368f217\'), ObjectId(\'62c56bbdba7f1d001368f217\')]\"
],
\"tags\" : [
\"[MinKey, MaxKey]\"
],
\"deleted\" : [
\"[MinKey, true)\",
\"(true, MaxKey]\"
]
}
}
}
},
{
\"stage\" : \"PROJECTION_SIMPLE\",
\"transformBy\" : {
\"_id\" : true
},
\"inputStage\" : {
\"stage\" : \"FETCH\",
\"filter\" : {
\"$and\" : [
{
\"accountId\" : {
\"$eq\" : ObjectId(\"62c56bbef2c6530d4a9b5976\")
}
},
{
\"dataHash\" : {
\"$eq\" : \"00002478a5c9594923833b3534b2d0b17bb7298a\"
}
}
]
},
\"inputStage\" : {
\"stage\" : \"IXSCAN\",
\"keyPattern\" : {
\"businessId\" : 1.0,
\"documentId\" : 1.0,
\"status\" : 1.0,
\"deleted\" : 1.0
},
\"indexName\" : \"businessId_1_documentId_1_status_1_deleted_1\",
\"isMultiKey\" : false,
\"multiKeyPaths\" : {
\"businessId\" : [
],
\"documentId\" : [
],
\"status\" : [
],
\"deleted\" : [
]
},
\"isUnique\" : false,
\"isSparse\" : false,
\"isPartial\" : false,
\"indexVersion\" : 2.0,
\"direction\" : \"forward\",
\"indexBounds\" : {
\"businessId\" : [
\"[ObjectId(\'62c56bbdba7f1d001368f217\'), ObjectId(\'62c56bbdba7f1d001368f217\')]\"
],
\"documentId\" : [
\"[MinKey, MaxKey]\"
],
\"status\" : [
\"[MinKey, MaxKey]\"
],
\"deleted\" : [
\"[MinKey, true)\",
\"(true, MaxKey]\"
]
}
}
}
},
{
\"stage\" : \"PROJECTION_SIMPLE\",
\"transformBy\" : {
\"_id\" : true
},
\"inputStage\" : {
\"stage\" : \"FETCH\",
\"filter\" : {
\"$and\" : [
{
\"businessId\" : {
\"$eq\" : ObjectId(\"62c56bbdba7f1d001368f217\")
}
},
{
\"dataHash\" : {
\"$eq\" : \"00002478a5c9594923833b3534b2d0b17bb7298a\"
}
}
]
},
\"inputStage\" : {
\"stage\" : \"IXSCAN\",
\"keyPattern\" : {
\"accountId\" : 1.0,
\"description\" : 1.0,
\"address.name\" : 1.0,
\"deleted\" : 1.0
},
\"indexName\" : \"accountId_1_description_1_address.name_1_deleted_1\",
\"isMultiKey\" : false,
\"multiKeyPaths\" : {
\"accountId\" : [
],
\"description\" : [
],
\"address.name\" : [
],
\"deleted\" : [
]
},
\"isUnique\" : false,
\"isSparse\" : false,
\"isPartial\" : false,
\"indexVersion\" : 2.0,
\"direction\" : \"forward\",
\"indexBounds\" : {
\"accountId\" : [
\"[ObjectId(\'62c56bbef2c6530d4a9b5976\'), ObjectId(\'62c56bbef2c6530d4a9b5976\')]\"
],
\"description\" : [
\"[MinKey, MaxKey]\"
],
\"address.name\" : [
\"[MinKey, MaxKey]\"
],
\"deleted\" : [
\"[MinKey, true)\",
\"(true, MaxKey]\"
]
}
}
}
},
{
\"stage\" : \"PROJECTION_SIMPLE\",
\"transformBy\" : {
\"_id\" : true
},
\"inputStage\" : {
\"stage\" : \"FETCH\",
\"filter\" : {
\"$and\" : [
{
\"businessId\" : {
\"$eq\" : ObjectId(\"62c56bbdba7f1d001368f217\")
}
},
{
\"dataHash\" : {
\"$eq\" : \"00002478a5c9594923833b3534b2d0b17bb7298a\"
}
}
]
},
\"inputStage\" : {
\"stage\" : \"IXSCAN\",
\"keyPattern\" : {
\"accountId\" : 1.0,
\"timestamp\" : 1.0,
\"amount\" : 1.0,
\"type\" : 1.0,
\"deleted\" : 1.0
},
\"indexName\" : \"accountId_1_timestamp_1_amount_1_type_1_deleted_1\",
\"isMultiKey\" : false,
\"multiKeyPaths\" : {
\"accountId\" : [
],
\"timestamp\" : [
],
\"amount\" : [
],
\"type\" : [
],
\"deleted\" : [
]
},
\"isUnique\" : false,
\"isSparse\" : false,
\"isPartial\" : false,
\"indexVersion\" : 2.0,
\"direction\" : \"forward\",
\"indexBounds\" : {
\"accountId\" : [
\"[ObjectId(\'62c56bbef2c6530d4a9b5976\'), ObjectId(\'62c56bbef2c6530d4a9b5976\')]\"
],
\"timestamp\" : [
\"[MinKey, MaxKey]\"
],
\"amount\" : [
\"[MinKey, MaxKey]\"
],
\"type\" : [
\"[MinKey, MaxKey]\"
],
\"deleted\" : [
\"[MinKey, true)\",
\"(true, MaxKey]\"
]
}
}
}
},
{
\"stage\" : \"PROJECTION_SIMPLE\",
\"transformBy\" : {
\"_id\" : true
},
\"inputStage\" : {
\"stage\" : \"FETCH\",
\"filter\" : {
\"dataHash\" : {
\"$eq\" : \"00002478a5c9594923833b3534b2d0b17bb7298a\"
}
},
\"inputStage\" : {
\"stage\" : \"IXSCAN\",
\"keyPattern\" : {
\"businessId\" : 1.0,
\"accountId\" : 1.0,
\"deleted\" : 1.0
},
\"indexName\" : \"businessId_1_accountId_1_deleted_1\",
\"isMultiKey\" : false,
\"multiKeyPaths\" : {
\"businessId\" : [
],
\"accountId\" : [
],
\"deleted\" : [
]
},
\"isUnique\" : false,
\"isSparse\" : false,
\"isPartial\" : false,
\"indexVersion\" : 2.0,
\"direction\" : \"forward\",
\"indexBounds\" : {
\"businessId\" : [
\"[ObjectId(\'62c56bbdba7f1d001368f217\'), ObjectId(\'62c56bbdba7f1d001368f217\')]\"
],
\"accountId\" : [
\"[ObjectId(\'62c56bbef2c6530d4a9b5976\'), ObjectId(\'62c56bbef2c6530d4a9b5976\')]\"
],
\"deleted\" : [
\"[MinKey, true)\",
\"(true, MaxKey]\"
]
}
}
}
},
{
\"stage\" : \"PROJECTION_SIMPLE\",
\"transformBy\" : {
\"_id\" : true
},
\"inputStage\" : {
\"stage\" : \"FETCH\",
\"filter\" : {
\"dataHash\" : {
\"$eq\" : \"00002478a5c9594923833b3534b2d0b17bb7298a\"
}
},
\"inputStage\" : {
\"stage\" : \"IXSCAN\",
\"keyPattern\" : {
\"businessId\" : 1.0,
\"accountId\" : 1.0,
\"vendorId\" : 1.0,
\"category.code\" : 1.0,
\"deleted\" : 1.0
},
\"indexName\" : \"businessId_1_accountId_1_vendorId_1_category.code_1_deleted_1\",
\"isMultiKey\" : false,
\"multiKeyPaths\" : {
\"businessId\" : [
],
\"accountId\" : [
],
\"vendorId\" : [
],
\"category.code\" : [
],
\"deleted\" : [
]
},
\"isUnique\" : false,
\"isSparse\" : false,
\"isPartial\" : false,
\"indexVersion\" : 2.0,
\"direction\" : \"forward\",
\"indexBounds\" : {
\"businessId\" : [
\"[ObjectId(\'62c56bbdba7f1d001368f217\'), ObjectId(\'62c56bbdba7f1d001368f217\')]\"
],
\"accountId\" : [
\"[ObjectId(\'62c56bbef2c6530d4a9b5976\'), ObjectId(\'62c56bbef2c6530d4a9b5976\')]\"
],
\"vendorId\" : [
\"[MinKey, MaxKey]\"
],
\"category.code\" : [
\"[MinKey, MaxKey]\"
],
\"deleted\" : [
\"[MinKey, true)\",
\"(true, MaxKey]\"
]
}
}
}
},
{
\"stage\" : \"PROJECTION_SIMPLE\",
\"transformBy\" : {
\"_id\" : true
},
\"inputStage\" : {
\"stage\" : \"FETCH\",
\"filter\" : {
\"$and\" : [
{
\"accountId\" : {
\"$eq\" : ObjectId(\"62c56bbef2c6530d4a9b5976\")
}
},
{
\"businessId\" : {
\"$eq\" : ObjectId(\"62c56bbdba7f1d001368f217\")
}
}
]
},
\"inputStage\" : {
\"stage\" : \"IXSCAN\",
\"keyPattern\" : {
\"dataHash\" : 1.0,
\"deleted\" : 1.0
},
\"indexName\" : \"dataHash_1_deleted_1\",
\"isMultiKey\" : false,
\"multiKeyPaths\" : {
\"dataHash\" : [
],
\"deleted\" : [
]
},
\"isUnique\" : false,
\"isSparse\" : false,
\"isPartial\" : false,
\"indexVersion\" : 2.0,
\"direction\" : \"forward\",
\"indexBounds\" : {
\"dataHash\" : [
\"[\\\"00002478a5c9594923833b3534b2d0b17bb7298a\\\", \\\"00002478a5c9594923833b3534b2d0b17bb7298a\\\"]\"
],
\"deleted\" : [
\"[MinKey, true)\",
\"(true, MaxKey]\"
]
}
}
}
},
{
\"stage\" : \"PROJECTION_SIMPLE\",
\"transformBy\" : {
\"_id\" : true
},
\"inputStage\" : {
\"stage\" : \"FETCH\",
\"filter\" : {
\"$and\" : [
{
\"accountId\" : {
\"$eq\" : ObjectId(\"62c56bbef2c6530d4a9b5976\")
}
},
{
\"dataHash\" : {
\"$eq\" : \"00002478a5c9594923833b3534b2d0b17bb7298a\"
}
}
]
},
\"inputStage\" : {
\"stage\" : \"IXSCAN\",
\"keyPattern\" : {
\"businessId\" : 1.0,
\"address.name\" : 1.0,
\"status\" : 1.0,
\"description\" : 1.0,
\"vendorId\" : 1.0,
\"deleted\" : 1.0
},
\"indexName\" : \"businessId_1_address.name_1_status_1_description_1_vendorId_1_deleted_1\",
\"isMultiKey\" : false,
\"multiKeyPaths\" : {
\"businessId\" : [
],
\"address.name\" : [
],
\"status\" : [
],
\"description\" : [
],
\"vendorId\" : [
],
\"deleted\" : [
]
},
\"isUnique\" : false,
\"isSparse\" : false,
\"isPartial\" : false,
\"indexVersion\" : 2.0,
\"direction\" : \"forward\",
\"indexBounds\" : {
\"businessId\" : [
\"[ObjectId(\'62c56bbdba7f1d001368f217\'), ObjectId(\'62c56bbdba7f1d001368f217\')]\"
],
\"address.name\" : [
\"[MinKey, MaxKey]\"
],
\"status\" : [
\"[MinKey, MaxKey]\"
],
\"description\" : [
\"[MinKey, MaxKey]\"
],
\"vendorId\" : [
\"[MinKey, MaxKey]\"
],
\"deleted\" : [
\"[MinKey, true)\",
\"(true, MaxKey]\"
]
}
}
}
},
{
\"stage\" : \"PROJECTION_SIMPLE\",
\"transformBy\" : {
\"_id\" : true
},
\"inputStage\" : {
\"stage\" : \"FETCH\",
\"inputStage\" : {
\"stage\" : \"IXSCAN\",
\"keyPattern\" : {
\"businessId\" : 1.0,
\"accountId\" : 1.0,
\"dataHash\" : 1.0,
\"deleted\" : 1.0
},
\"indexName\" : \"businessId_1_accountId_1_dataHash_1_deleted_1\",
\"isMultiKey\" : false,
\"multiKeyPaths\" : {
\"businessId\" : [
],
\"accountId\" : [
],
\"dataHash\" : [
],
\"deleted\" : [
]
},
\"isUnique\" : false,
\"isSparse\" : false,
\"isPartial\" : false,
\"indexVersion\" : 2.0,
\"direction\" : \"forward\",
\"indexBounds\" : {
\"businessId\" : [
\"[ObjectId(\'62c56bbdba7f1d001368f217\'), ObjectId(\'62c56bbdba7f1d001368f217\')]\"
],
\"accountId\" : [
\"[ObjectId(\'62c56bbef2c6530d4a9b5976\'), ObjectId(\'62c56bbef2c6530d4a9b5976\')]\"
],
\"dataHash\" : [
\"[\\\"00002478a5c9594923833b3534b2d0b17bb7298a\\\", \\\"00002478a5c9594923833b3534b2d0b17bb7298a\\\"]\"
],
\"deleted\" : [
\"[MinKey, true)\",
\"(true, MaxKey]\"
]
}
}
}
}
]
},
\"command\" : {
\"find\" : \"transactions\",
\"filter\" : {
\"businessId\" : ObjectId(\"62c56bbdba7f1d001368f217\"),
\"accountId\" : ObjectId(\"62c56bbef2c6530d4a9b5976\"),
\"dataHash\" : \"00002478a5c9594923833b3534b2d0b17bb7298a\",
\"deleted\" : {
\"$ne\" : true
}
},
\"projection\" : {
\"_id\" : true
},
\"$db\" : \"stage\"
},
\"serverInfo\" : {
\"host\" : \"cluster0-shard-00-01.u0b4v.mongodb.net\",
\"port\" : 27017.0,
\"version\" : \"5.0.12\",
\"gitVersion\" : \"79cfcdd83eb6f64e164a588d0daf9bb873328b45\"
},
\"serverParameters\" : {
\"internalQueryFacetBufferSizeBytes\" : 104857600.0,
\"internalQueryFacetMaxOutputDocSizeBytes\" : 104857600.0,
\"internalLookupStageIntermediateDocumentMaxSizeBytes\" : 16793600.0,
\"internalDocumentSourceGroupMaxMemoryBytes\" : 104857600.0,
\"internalQueryMaxBlockingSortMemoryUsageBytes\" : 33554432.0,
\"internalQueryProhibitBlockingMergeOnMongoS\" : 0.0,
\"internalQueryMaxAddToSetBytes\" : 104857600.0,
\"internalDocumentSourceSetWindowFieldsMaxMemoryBytes\" : 104857600.0
},
\"ok\" : 1.0,
\"$clusterTime\" : {
\"clusterTime\" : Timestamp(1663107520, 28),
\"signature\" : {
\"hash\" : BinData(0, \"E5Mndz8gTLw14zfsCVW0yZAa9hg=\"),
\"keyId\" : NumberLong(7083587916596772929)
}
},
\"operationTime\" : Timestamp(1663107520, 28)
}
中奖计划索引为businessId_1_reason_1_deleted_1,因此仅使用businessId 进行索引。当然我可以使用 .hint(),但是……为什么会这样?我阅读了文档,但没有找到答案。它应该工作
-
mongoplayground.net 选择了人们可能期望的索引/计划,这很奇怪也很有趣。
标签: mongodb database-indexes mongodb-indexes