Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-33966

redundant $sort in aggregation prevents best $limit $sort consolidation

    XMLWordPrintable

    Details

    • Backwards Compatibility:
      Minor Change
    • Backport Requested:
      v4.7, v4.4
    • Sprint:
      Query 2020-06-29, Query 2020-07-13, Query 2020-07-27
    • Case:
    • Linked BF Score:
      16

      Description

      If there is an extra $sort that's identical to one before it, and there's a $limit after it, it causes an unfortunate full $sort of every document before the second sort which absorbed $limit is executed.

      db.c.explain().aggregate([ {$sort: {a: 1}},{$sort:{a:1}},{$limit:5}]);
      {
      	"stages" : [
      		{
      			"$cursor" : {
      				"query" : {
       
      				},
      				"sort" : {
      					"a" : 1
      				},
      				"queryPlanner" : {
      					"plannerVersion" : 1,
      					"namespace" : "test.c",
      					"indexFilterSet" : false,
      					"parsedQuery" : {
       
      					},
      					"winningPlan" : {
      						"stage" : "EOF"
      					},
      					"rejectedPlans" : [ ]
      				}
      			}
      		},
      		{
      			"$sort" : {
      				"sortKey" : {
      					"a" : 1
      				},
      				"limit" : NumberLong(5)
      			}
      		}
      	],
      	"ok" : 1
      }
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              ribhav.jain Ribhav Jain (Inactive)
              Reporter:
              asya Asya Kamsky
              Participants:
              Votes:
              0 Vote for this issue
              Watchers:
              11 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: