分片集群上的MongoDB skip()和limit()

MongoDB skip() and limit() on a sharded cluster

如果我在分片的mongodb集群上运行以下查询,并且查询是分散/聚集类型。

1
find({"product":"laptop"}).sort({"year": 1}).skip(15).limit(5)

和跳过限制(组合)是否适用于mongos或单个分片(即mongod)?

或mongos向每个分片发送limit(20),即sum(skip limit)以优化查询,然后再应用
在返回到client

之前,先跳过(15).limit(5)到收集的结果


我想我找到了您的疑问的答案。您可以在此处阅读整个文档部分,但您感兴趣的部分是这一部分:

How mongos Handles Query Modifiers

Sorting

If the result of the query
is not sorted, the mongos instance opens a result cursor that a€?round
robinsa€? results from all cursors on the shards.

Limits

If the query limits the size of the result set using the
limit() cursor method, the mongos instance passes that limit to the
shards and then re-applies the limit to the result before returning
the result to the client.

Skips

If the query specifies a number of records to skip using the
skip() cursor method, the mongos cannot pass the skip to the shards,
but rather retrieves unskipped results from the shards and skips the
appropriate number of documents when assembling the complete result.

When used in conjunction with a limit(), the mongos will pass the
limit plus the value of the skip() to the shards to improve the
efficiency of these operations.

如果您担心性能,最好的办法是使用explain获取查询计划。在同一文档上,指定为:

For more information on how the work of aggregation is split among
components of a sharded cluster query, use explain:true as a parameter
to the aggregation() call. The return will include three json objects.
mergeType shows where the stage of the merge happens (a€?primarySharda€?,
a€?anySharda€?, or a€?mongosa€?). splitPipeline shows which operations in your
pipeline have run on individual shards. shards shows the work each
shard has done.