[SERVER-4187] automatically pick sparse indexes for $exists:true queries Created: 01/Nov/11  Updated: 28/Jan/15  Resolved: 28/Jan/15

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

Type: New Feature Priority: Major - P3
Reporter: Aaron Staple Assignee: David Storch
Resolution: Duplicate Votes: 10
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-10026 New query system Closed
is duplicated by SERVER-4578 $exists and sparse index Closed
is duplicated by SERVER-4543 $exists: true does not use sparse index Closed
Related
related to SERVER-12557 $exists could use index Closed
Participants:

 Description   

A standard index won't tell us if a field is missing or null, but a sparse index only contains entries for fields that are present.



 Comments   
Comment by David Storch [ 28/Jan/15 ]

Fixed by the rewrite of the query planner for 2.6. The new query system considers solving {$exists: true} queries by scanning sparse indices:

> db.version()
2.6.7
> t = db.t
test.t
> t.drop()
true
> t.ensureIndex({a: 1}, {sparse: true})
{
	"createdCollectionAutomatically" : true,
	"numIndexesBefore" : 1,
	"numIndexesAfter" : 2,
	"ok" : 1
}
> t.ensureIndex({b: 1})
{
	"createdCollectionAutomatically" : false,
	"numIndexesBefore" : 2,
	"numIndexesAfter" : 3,
	"ok" : 1
}
> t.insert({a: 1})
WriteResult({ "nInserted" : 1 })
> t.insert({b: 1})
WriteResult({ "nInserted" : 1 })
> t.find({a: {$exists: true}}).explain()
{
	"cursor" : "BtreeCursor a_1",
	"isMultiKey" : false,
	"n" : 1,
	"nscannedObjects" : 1,
	"nscanned" : 1,
	"nscannedObjectsAllPlans" : 1,
	"nscannedAllPlans" : 1,
	"scanAndOrder" : false,
	"indexOnly" : false,
	"nYields" : 0,
	"nChunkSkips" : 0,
	"millis" : 0,
	"indexBounds" : {
		"a" : [
			[
				{
					"$minElement" : 1
				},
				{
					"$maxElement" : 1
				}
			]
		]
	},
	"server" : "dstorch-desktop:27017",
	"filterSet" : false
}

Resolving as a duplicate of SERVER-10026.

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