[SERVER-25830] $comment on an empty query causes a collection scan Created: 26/Aug/16  Updated: 26/Aug/16  Resolved: 26/Aug/16

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

Type: Bug Priority: Critical - P2
Reporter: Bradley Arsenault Assignee: Max Hirschhorn
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-20536 Combining $comment and .count() degra... Closed
Operating System: ALL
Steps To Reproduce:

create a huge database
db.your_collection.count({$comment: "your_mom"});

Wait patiently

Participants:

 Description   

Take a look at this operation:

		{
			"opid" : 1453296,
			"active" : true,
			"secs_running" : 238,
			"microsecs_running" : NumberLong(238006204),
			"op" : "query",
			"ns" : "sensibill_loadtest.receipts",
			"query" : {
				"count" : "receipts",
				"query" : {
					"$comment" : {
						"source" : "admin.js:383",
						"version" : "d369cd29ba35"
					}
				},
				"fields" : null
			},
			"planSummary" : "COLLSCAN",
			"client" : "10.114.185.210:42439",
			"desc" : "conn155298",
			"threadId" : "0x7ebd51d57700",
			"connectionId" : 155298,
			"waitingForLock" : false,
			"numYields" : 95285,
			"lockStats" : {
				"timeLockedMicros" : {
					"r" : NumberLong(275342977),
					"w" : NumberLong(0)
				},
				"timeAcquiringMicros" : {
					"r" : NumberLong(641132),
					"w" : NumberLong(0)
				}
			}
		},

We have code that adds a $comment to every query that we send to Mongo, so that we can track where it came from within our system.

Normally it does not change results. However, on this empty receipts.count() query, it triggers a collection scan. Arggghhh!



 Comments   
Comment by Max Hirschhorn [ 26/Aug/16 ]

Hi genixpro,

Thank you for reporting this issue! As part of SERVER-20536, we made the the "count" command ignore the $comment field in deciding whether we can use the fast-count code path. This fix will be included in the upcoming 3.4 release. I'm therefore going to close this ticket as a duplicate of SERVER-20536.

I've included the explain output of your "count" command example when running against a 3.3.11 development release of MongoDB to show there is no longer a COLLSCAN stage and that we don't examine any documents.

> db.runCommand({  "explain" : {  "count" : "receipts",  "query" : {  "$comment" : {  "source" : "admin.js:383",  "version" : "d369cd29ba35" } },  "fields" : null } })
{
  "queryPlanner" : {
    "plannerVersion" : 1,
    "namespace" : "test.receipts",
    "indexFilterSet" : false,
    "winningPlan" : {
      "stage" : "COUNT"
    },
    "rejectedPlans" : [ ]
  },
  "executionStats" : {
    "executionSuccess" : true,
    "nReturned" : 0,
    "executionTimeMillis" : 0,
    "totalKeysExamined" : 0,
    "totalDocsExamined" : 0,
    "executionStages" : {
      "stage" : "COUNT",
      "nReturned" : 0,
      "executionTimeMillisEstimate" : 0,
      "works" : 1,
      "advanced" : 0,
      "needTime" : 0,
      "needYield" : 0,
      "saveState" : 0,
      "restoreState" : 0,
      "isEOF" : 1,
      "invalidates" : 0,
      "nCounted" : 5,
      "nSkipped" : 0
    },
    "allPlansExecution" : [ ]
  },
  "serverInfo" : {
    "version" : "3.3.11",
    "gitVersion" : "4e682143bca253a840f07604c0d7c522d48b5c29"
  },
  "ok" : 1
}

Thanks,
Max

Generated at Thu Feb 08 04:10:21 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.