[SERVER-16555] mongoS changes projections for queries for unsharded collections unnecessarily Created: 15/Dec/14  Updated: 28/Oct/15  Resolved: 23/Oct/15

Status: Closed
Project: Core Server
Component/s: Querying, Sharding
Affects Version/s: 2.6.4
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Charlie Page Assignee: David Storch
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
Participants:

 Description   

In a sharded cluster, but using an unsharded collection, mongoS, given the query:

mongos.getDB("db").foo.find({a:1}, {a:1}).sort({b:1})
Index:{a:1}

Will rewrite to:

.find({a:1}, {a:1, b:1}).sort({b:1})

This can be observed by running the mongoD at logLevel:2 or greater.

As this is targeting a single shard, the query can run as is, mongoD will return everything in correct sorted order.

Two possible answers:

  1. This is the correct behavior, we will always rewrite (and accept the cost of adding projections). Better for query plan cache filters.
  2. This isn't correct behavior and when a single shard is targeted the query shouldn't be rewritten. Better for speed and efficiency.


 Comments   
Comment by David Storch [ 23/Oct/15 ]

This was fixed under SERVER-15176. Resolving as a duplicate.

As part of the rewrite of the mongos query path, we changed the way that mongos obtains sort keys for performing its sorted merge. I explained this in my comment on SERVER-20987. Quoting from that ticket:

The new query path instead issues a {$meta: "sortKey"} projection to the shards. (Sort key meta-projection was added in the 3.1 development cycle explicitly for this purpose.) This instructs each shard to return the sort key it generated for each result document as part of its response. Mongos then uses this sort key metadata to perform its mergesort. It then strips out the metadata before returning the result set to the client. This work was done under SERVER-19355.

The addition of sortKey $meta projection means that mongos no longer rewrites projections.

Generated at Thu Feb 08 03:41:26 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.