MongoDB 操作顺序和更改获胜计划
MongoDB operations order and changing winning plan
我目前 运行 遇到了一些收集数百份(大量)文档的问题。
文档的某些字段比较重,我们在投影阶段将其排除。
我们发现排序实际上是在投影之前完成的,导致了一些内存问题:
Overflow sort stage buffered data usage of 33658319 bytes exceeds internal limit of 33554432 bytes
问题如下:
- 是否可以手动指定中标方案?
- 是否可以在排序前做投影?
- 为什么先排序再放映?
是否可以指定中标方案?
我觉得你不能直接指定中奖方案,但是你可以使用cursor.hint()
到select一个索引在你的查询中使用,这会影响中奖方案。例如,如果要进行完整的集合扫描,请使用 { $natural : 1 }
.
请记住,从 MongoDB 2.6
开始,如果您的查询形状存在索引过滤器(query
、sort
和 project
).
是否可以先投影再排序?
是的,您可以使用 aggregation framework 指定操作顺序,并在 $sort
之前执行 $project
。
为什么先排序再投影?
如果排序在$project
、$unwind
、$group
之前完成,可以利用索引。否则不能。 From the docs:
$sort Operator and Performance
$sort
operator can take advantage of an index when placed at the beginning of the pipeline or placed before the $project
, $unwind
, and $group
aggregation operators. If $project
, $unwind
, or $group
occur prior to the $sort
operation, $sort
cannot use any indexes.
我目前 运行 遇到了一些收集数百份(大量)文档的问题。
文档的某些字段比较重,我们在投影阶段将其排除。
我们发现排序实际上是在投影之前完成的,导致了一些内存问题:
Overflow sort stage buffered data usage of 33658319 bytes exceeds internal limit of 33554432 bytes
问题如下:
- 是否可以手动指定中标方案?
- 是否可以在排序前做投影?
- 为什么先排序再放映?
是否可以指定中标方案?
我觉得你不能直接指定中奖方案,但是你可以使用cursor.hint()
到select一个索引在你的查询中使用,这会影响中奖方案。例如,如果要进行完整的集合扫描,请使用 { $natural : 1 }
.
请记住,从 MongoDB 2.6
开始,如果您的查询形状存在索引过滤器(query
、sort
和 project
).
是否可以先投影再排序?
是的,您可以使用 aggregation framework 指定操作顺序,并在 $sort
之前执行 $project
。
为什么先排序再投影?
如果排序在$project
、$unwind
、$group
之前完成,可以利用索引。否则不能。 From the docs:
$sort Operator and Performance
$sort
operator can take advantage of an index when placed at the beginning of the pipeline or placed before the$project
,$unwind
, and$group
aggregation operators. If$project
,$unwind
, or$group
occur prior to the$sort
operation,$sort
cannot use any indexes.