MongoDB Aggregation Pipeline 并没有使用索引
資深大佬 : JasonLaw 0
我有一个拥有五百万个文档的集合,文档的结构如下:
{ "_id" : ObjectId("5ed23bd292845eaf1e488965"), "categoryId" : 18790, "targetType" : 43, "targetId" : 433, "priority" : 0 }
集合拥有索引categoryId_1_priority_-1
。
我期望如下的 Aggregation Pipeline 使用到索引,但是事实上并没有。
db.collection.aggregate([{$facet: {"category_1_order_by_priority_limit_2": [{$match: {categoryId: 1}}, {$sort: {priority: -1}}, {$limit: 2}],"category_2_order_by_priority_limit_3": [{$match: {categoryId: 2}}, {$sort: {priority: -1}}, {$limit: 3}]}}])
explain 的结果如下:
{ "stages" : [ { "$cursor" : { "query" : { }, "queryPlanner" : { "plannerVersion" : 1, "namespace" : "test.collection", "indexFilterSet" : false, "parsedQuery" : { }, "queryHash" : "8B3D4AB8", "planCacheKey" : "8B3D4AB8", "winningPlan" : { "stage" : "COLLSCAN", "direction" : "forward" }, "rejectedPlans" : [ ] } } }, { "$facet" : { ... } } ], "serverInfo" : { ... }, "ok" : 1 }
我应该怎么做才能使用到索引呢?
indexing – MongoDB Aggregation Pipeline $match $sort doesn’t use index – Stack Overflow
大佬有話說 (2)