[JAVA-2104] Add explain() capability for new Java driver API Created: 04/Feb/16  Updated: 01/Sep/20  Resolved: 08/Nov/17

Status: Closed
Project: Java Driver
Component/s: Query Operations
Affects Version/s: 3.2.1
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Andrew Cuga [X] Assignee: Unassigned
Resolution: Done Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Backports
backports DOCS-11634 Improve Performance with Indexes Closed
Related
related to JAVA-2105 "$explain" in FindOptions.modifiers i... Closed
is related to JAVA-2427 Deprecate modifiers in FindOptions an... Closed

 Description   

There appears to be no equivalent of the explain() method on the new API's find() operation, or any other discoverable place in the new API.



 Comments   
Comment by Mike Zraly [ 01/Sep/20 ]

I checked:

Setting notablescan does not cause failures when a plan has a blocking sort.  A query plan that looks like

SORT
SORT_KEY_GENERATOR
FETCH
IXSCAN index:i_1

works just fine.  After dropping the index the query plan would have been 

SORT
SORT_KEY_GENERATOR
COLLSCAN filter:...

and that does fail because of the COLLSCAN stage.

Another observation is that queries with no predicate or the empty predicate {} do NOT trigger a failure when the notablescan option is set.  OT1H this is nice because it lets us scan the entire collection when we need to.  OTOH this is not nice because we might inadvertently leave out the query predicate and notablescan won't catch that when it's an error.

Given the observations above I'd rather have the option of writing an explicit test that checks for SORT and COLLSCAN stages in explain output.

 

Comment by Jeffrey Yemin [ 01/Sep/20 ]

mike.zraly@sophos.com

As an alternative, https://docs.mongodb.com/manual/reference/parameters/#param.notablescan may be of use to you.  This will force the server to error if a full collection scan is required to execute a query.  I'm not sure if it also errors on blocking sorts.

Comment by Ian Springer [ 31/Aug/20 ]

 

Comment by Mike Zraly [ 31/Aug/20 ]

Not sure if anyone will see this, but I should point out that it is helpful to be able to execute explain() in Java test code to make sure we are not executing a COLLSCAN or a blocking SORT, understanding that the test case has to have enough variety of documents in the database to get a realistic query plan.

In any case, if we're expected to use the .modifiers() method to request explain output, can we please not deprecate that method?

Comment by Andrew Cuga [X] [ 04/Feb/16 ]

I'm happy as long as the capability exists, as you point out it does. Thanks for the code snippet showing how to use the "$explain" modifier. I was trying to supply modifiers such as this but couldn't guess the syntax / find it in the documentation.

Then again, as we're on 3.2.1, it looks like we'll need to wait to run `explain()` in Java as the issue points out it's being ignored. Thanks for pointing that issue out and for the help!

Comment by Jeffrey Yemin [ 04/Feb/16 ]

Hi Andrew,

It's true that the new CRUD API does not include helpers for explain. The justification is provided in the specification Q & A.

As mentioned in the specification, you can continue to use the "$explain" modifier for this, e.g.

collection.find().modifiers(new Document("$explain", true)).first();

Alternatively, you can execute the explain command directly via MongoDatabase.runCommand.

While investigating this issue, I discovered two other issues:

  • JAVA-2105: The $explain modifier is ignored when connected to a server with version >= 3.2
  • DOCS-7096: The explain command documentation does not mention the find command introduced in the 3.2 server

If you still feel strongly that drivers should provide helpers for explain, please make a request in the DRIVERS Jira project.

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