[SERVER-20536] Combining $comment and .count() degrades performance Created: 21/Sep/15  Updated: 28/Nov/16  Resolved: 05/Feb/16

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

Type: Bug Priority: Major - P3
Reporter: Alon Horev Assignee: Charlie Swanson
Resolution: Done Votes: 0
Labels: neweng
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
is duplicated by SERVER-25830 $comment on an empty query causes a c... Closed
Related
is related to DOCS-9392 3.4: Behavior change for count with h... Closed
Backwards Compatibility: Major Change
Operating System: ALL
Sprint: Query 10 (02/22/16)
Participants:

 Description   

When passing a $comment in a query count() doesn't use an index.

Testcase:

> for (var i=0; i < 1000000; i++) db.test.insert({a:i})
> db.test.count()
1000001
> db.test.count({$comment:"A"})
1000001
> var start = new ISODate(); db.test.count(); new ISODate() - start
0
> var start = new ISODate(); db.test.count({$comment:"A"}); new ISODate() - start
155



 Comments   
Comment by Charlie Swanson [ 05/Feb/16 ]

This is a potentially backwards breaking change. If you specify a hint to use a sparse index with an empty count predicate, it used to ignore the hint. It will now respect the hint, potentially giving you the wrong count. We may want to add this to the compatibility notes?

For example:

> db.foo.drop()
true
> db.foo.insert({_id: 0})
WriteResult({ "nInserted" : 1 })
> db.foo.ensureIndex({x: 1}, {sparse: true});
{
	"createdCollectionAutomatically" : false,
	"numIndexesBefore" : 1,
	"numIndexesAfter" : 2,
	"ok" : 1
}
> db.foo.find({}).hint({x: 1}).count()
0  // Used to ignore the hint and return 1.

Comment by Githook User [ 05/Feb/16 ]

Author:

{u'username': u'cswanson310', u'name': u'Charlie Swanson', u'email': u'charlie.swanson@mongodb.com'}

Message: SERVER-20536 Ignore comment field when planning counts.
Branch: master
https://github.com/mongodb/mongo/commit/ebc481d27e408d8608075a61eba2a68be43c1314

Comment by J Rassi [ 21/Sep/15 ]

Thanks for the report, Alon.

It looks like we currently fail to apply the Collection::numRecords() optimization for the count command if the query object is non-empty. Setting fixVersion to "Needs Triage"; we'll take a deeper look at this at our next triage meeting.

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