[SERVER-19507] Distinct command should use covered query plan when distinct field is a non-first element of index key pattern Created: 21/Jul/15  Updated: 02/Sep/16  Resolved: 25/Aug/16

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: None
Fix Version/s: 3.3.12

Type: Improvement Priority: Major - P3
Reporter: J Rassi Assignee: David Storch
Resolution: Done Votes: 14
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
Backwards Compatibility: Fully Compatible
Sprint: Query 2016-08-29
Participants:

 Description   

Compound indexes should be able to support covered distinct queries, where the distinct key is a non-first element of the index key pattern. Distinct queries are able to use these indexes, but only non-covered plans are generated.

Reproduce with the following:

> var l = db.setLogLevel(5,'query')
> db.foo.drop()
true
> db.foo.ensureIndex({a:1,b:1})
{
	"createdCollectionAutomatically" : true,
	"numIndexesBefore" : 1,
	"numIndexesAfter" : 2,
	"ok" : 1
}
> db.foo.distinct("b",{a:1}) // Should use covered plan on {a:1,b:1}.

From the verbose query log output below, it can be seen that a FETCH <= IXSCAN plan is generated. The FETCH stage here is unnecessary, as the index already contains all information needed in order to generate the set of distinct keys for the requested field.

2015-07-21T13:50:57.081-0400 D QUERY    [conn4] Planner: adding solution:
FETCH
---fetched = 1
---sortedByDiskLoc = 0
---getSort = [{ a: 1 }, { a: 1, b: 1 }, { b: 1 }, ]
---Child:
------IXSCAN
---------keyPattern = { a: 1.0, b: 1.0 }
---------direction = 1
---------bounds = field #0['a']: [1.0, 1.0], field #1['b']: [MinKey, MaxKey]
---------fetched = 0
---------sortedByDiskLoc = 0
---------getSort = [{ a: 1 }, { a: 1, b: 1 }, { b: 1 }, ]



 Comments   
Comment by Githook User [ 25/Aug/16 ]

Author:

{u'username': u'Shekeen', u'name': u'Anton Guryanov', u'email': u'guryanov91@gmail.com'}

Message: SERVER-19507 allow DISTINCT_SCAN over trailing field of an index

Closes #1101

Signed-off-by: David Storch <david.storch@10gen.com>
Branch: master
https://github.com/mongodb/mongo/commit/1101287cc7789e9b8b52ba370cee55678aa6c58d

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