[SERVER-8346] Allow Query Hints by Index Name Created: 25/Jan/13  Updated: 07/Mar/14  Resolved: 25/Jan/13

Status: Closed
Project: Core Server
Component/s: Index Maintenance, Querying, Usability
Affects Version/s: 2.2.1
Fix Version/s: None

Type: Improvement Priority: Minor - P4
Reporter: Reuben Garrett Assignee: Unassigned
Resolution: Done Votes: 0
Labels: indexing, mongos, query
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to DOCS-1277 specify that can use hint with index ... Closed
Participants:

 Description   

Support Query Hinting by Index Name in the Mongo shell, as addressed in RUBY-493:

db.coll.ensureIndex({ k: 1 }, { name: "mySortKey" }); 
db.coll.save({ k: 1, v: "someValue" }); 
// ... insert more documents 
db.coll.find().hint("mySortKey"); 

I noticed an old ticket (SERVER-8) about removing index names as redundant. I don't know if that's still intended, and I do appreciate the necessity of simplicity. At the same time, I want to defend index names as an important means of hiding implementation details.

Sure, developers "should" normally know the structure of the indices they're using. However, in some cases we might just be using an index to ensure quick sorting on an abstract property, regardless of which document fields that property happens to cover. Query hints are a key area to realize the value of index names, because symbolic names can allow developers to vary index definition in response to "schema" adjustments (albeit Mongo is NoSQL, we still have pseudo-schemas) without impacting the optimization of downstream queries. And, given that field names are often optimized to a fine point just to reduce storage size (see SERVER-863), the field structure of an index tends to be far less meaningful than its name.

Indices are the pith of queries, and index names are the closest Mongo has to named views. Let's take them to the next level and allow hinting on index names!



 Comments   
Comment by Reuben Garrett [ 26/Jan/13 ]

I'm so sorry, scotthernandez - I did not test this adequately before opening a ticket. I apologize for the spurious ticket, and thank you for the functionality!

Comment by Scott Hernandez (Inactive) [ 25/Jan/13 ]

It already works this way:

> db.c.find().hint("_id_").explain()
{
	"cursor" : "BtreeCursor _id_",
	"isMultiKey" : false,
	"n" : 4,
	"nscannedObjects" : 4,
	"nscanned" : 4,
	"nscannedObjectsAllPlans" : 4,
	"nscannedAllPlans" : 4,
	"scanAndOrder" : false,
	"indexOnly" : false,
	"nYields" : 0,
	"nChunkSkips" : 0,
	"millis" : 0,
	"indexBounds" : {
		"_id" : [
			[
				{
					"$minElement" : 1
				},
				{
					"$maxElement" : 1
				}
			]
		]
	},
	"server" : "pans-2.local:27017"
}
> db.c.find().explain()
{
	"cursor" : "BasicCursor",
	"isMultiKey" : false,
	"n" : 4,
	"nscannedObjects" : 4,
	"nscanned" : 4,
	"nscannedObjectsAllPlans" : 4,
	"nscannedAllPlans" : 4,
	"scanAndOrder" : false,
	"indexOnly" : false,
	"nYields" : 0,
	"nChunkSkips" : 0,
	"millis" : 0,
	"indexBounds" : {
		
	},
	"server" : "pans-2.local:27017"
}

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