MongoDB 分片集群上的 skip() 和 limit()
MongoDB skip() and limit() on a sharded cluster
如果我 运行 在分片 mongodb 集群上进行查询并且查询是 scatter/gather 类型。
find({"product": "laptop"}).sort({"year": 1}).skip(15).limit(5)
将 SKIP + LIMITS(组合)应用于 mongos 或单个分片(即 mongod)?
或者 mongos 向每个分片发送 limit(20) 即 sum(skip+limit) 来优化查询然后也应用
skip(15).limit(5) 返回客户端之前收集的结果
我想我找到了你疑惑的答案。您可以阅读整个文档部分 here,但您感兴趣的部分是:
How mongos Handles Query Modifiers
Sorting
If the result of the query
is not sorted, the mongos
instance opens a result cursor that “round
robins” 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 (“primaryShard”,
“anyShard”, or “mongos”). splitPipeline shows which operations in your
pipeline have run on individual shards. shards shows the work each
shard has done.
如果我 运行 在分片 mongodb 集群上进行查询并且查询是 scatter/gather 类型。
find({"product": "laptop"}).sort({"year": 1}).skip(15).limit(5)
将 SKIP + LIMITS(组合)应用于 mongos 或单个分片(即 mongod)?
或者 mongos 向每个分片发送 limit(20) 即 sum(skip+limit) 来优化查询然后也应用 skip(15).limit(5) 返回客户端之前收集的结果
我想我找到了你疑惑的答案。您可以阅读整个文档部分 here,但您感兴趣的部分是:
How mongos Handles Query Modifiers
Sorting
If the result of the query is not sorted, the
mongos
instance opens a result cursor that “round robins” 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, themongos
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()
, themongos
will pass the limit plus the value of theskip()
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 theaggregation()
call. The return will include three json objects. mergeType shows where the stage of the merge happens (“primaryShard”, “anyShard”, or “mongos”). splitPipeline shows which operations in your pipeline have run on individual shards. shards shows the work each shard has done.