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

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

    • Minor Change
    • v4.7, v4.4
    • Query 2020-06-29, Query 2020-07-13, Query 2020-07-27
    • 16

      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
      }
      

            Assignee:
            ribhav.jain@mongodb.com Ribhav Jain (Inactive)
            Reporter:
            asya.kamsky@mongodb.com Asya Kamsky
            Votes:
            0 Vote for this issue
            Watchers:
            10 Start watching this issue

              Created:
              Updated:
              Resolved: