[SERVER-12938] Expose and Pretty Print CanonicalQuery Created: 27/Feb/14  Updated: 06/Dec/22  Resolved: 22/Feb/18

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

Type: Improvement Priority: Major - P3
Reporter: Stephen Lee Assignee: Backlog - Query Team (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-12486 shell helpers for plan cache commands Closed
related to SERVER-12692 Add cusor-style planCache helpers for... Closed
Assigned Teams:
Query
Participants:

 Description   

If I read the source correctly, CanonicalQuery is used as the [query] "plan cache key". It'd be nice to expose functionality to convert an arbitrary query to its shape.

Currently, it appears like strings like "aneqorderbyeqquery", "inb", or "oreqainb" are outputted for queries on a collection of documents containing fields "a" and "b" (where b is an array of integers).



 Comments   
Comment by Daniel Pasette (Inactive) [ 03/Mar/14 ]

Don't want to expose these internals at the moment. There is a workaround for the given use case for the time being.

Comment by J Rassi [ 27/Feb/14 ]

To me this functionality seems useful for diagnosing bugs in the process of converting a query to its shape.

Comment by Scott Hernandez (Inactive) [ 27/Feb/14 ]

What is the point of converting to its shape, how is that useful? Are you just looking for plans for a query (shape), as rassi@10gen.com has mentioned or maybe a nicer helper like this: SERVER-12692

Comment by J Rassi [ 27/Feb/14 ]

stephen.lee@10gen.com: minor nit, CanonicalQuery isn't used as a plan cache key; PlanCache key is. For example, {a:1} and {a:2} have non-equivalent representations in CanonicalQuery, but have an equivalent PlanCacheKey.

Would it fit your use case to have getPlansByQuery() include the PlanCacheKey for the query in its output? See below suggestion.

> db.foo.getPlanCache().getPlansByQuery({a:1})
[
	{
		"details" : {
			// XXX insert {"planCacheKey": "eqa"} here?
			"solution" : "(index-tagged expression tree: tree=Leaf { a: 1.0 }, pos: 0\n)"
		},
		"reason" : {
			"score" : 2,
			"stats" : {
				"type" : "FETCH",
				"works" : 1,
				"yields" : 0,
				"unyields" : 0,
				"invalidates" : 0,
				"advanced" : 0,
				"needTime" : 0,
				"needFetch" : 0,
				"isEOF" : 1,
				"alreadyHasObj" : 0,
				"forcedFetches" : 0,
				"matchTested" : 0,
				"children" : [
					{
						"type" : "IXSCAN",
						"works" : 1,
						"yields" : 0,
						"unyields" : 0,
						"invalidates" : 0,
						"advanced" : 0,
						"needTime" : 0,
						"needFetch" : 0,
						"isEOF" : 1,
						"keyPattern" : "{ a: 1.0 }",
						"boundsVerbose" : "field #0['a']: [1.0, 1.0]",
						"isMultiKey" : 0,
						"yieldMovedCursor" : 0,
						"dupsTested" : 0,
						"dupsDropped" : 0,
						"seenInvalidated" : 0,
						"matchTested" : 0,
						"keysExamined" : 0,
						"children" : [ ]
					}
				]
			}
		},
		"feedback" : {
			"nfeedback" : 0,
			"averageScore" : 0,
			"stdDevScore" : 0,
			"scores" : [ ]
		},
		"filterSet" : false
	},
	{
		"details" : {
			// XXX ditto here
			"solution" : "(collection scan)"
		},
		"reason" : {
			"score" : 2,
			"stats" : {
				"type" : "COLLSCAN",
				"works" : 1,
				"yields" : 0,
				"unyields" : 0,
				"invalidates" : 0,
				"advanced" : 0,
				"needTime" : 1,
				"needFetch" : 0,
				"isEOF" : 0,
				"docsTested" : 0,
				"children" : [ ]
			}
		},
		"feedback" : {
 
		},
		"filterSet" : false
	}
]
>

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