javascript - mongodb -- count() much slower than find()? -


i'm using mongoose count number of documents match query. index query is: {createdat: -1, status: -1, oid: -1}

mongo version 3.2, amount of docs in collection 1.75 million.

if do:

model.find({                          createdat: {'$gte': threemonths, '$lt': today},                          status: {'$in': model.status_set}                      }).select({_id: 0, status: 1}).count().then((c) => result[alias] = c)

it takes more 2 minutes. if do:

model.find({                  createdat: {'$gte': threemonths, '$lt': today},                  status: {'$in': model.status_set}              }).select({_id: 0, status: 1}).lean().then((c) => result[alias] = c.length)

then takes 2.5 seconds.

something i'm doing wrong? can speed things up?

edit: explain logs.

for count:

"executionstats" : {  		"executionsuccess" : true,  		"nreturned" : 0,  		"executiontimemillis" : 82671,  		"totalkeysexamined" : 1749689,  		"totaldocsexamined" : 1643722,  		"executionstages" : {  			"stage" : "count",  			"nreturned" : 0,  			"executiontimemillisestimate" : 80960,  			"works" : 1750066,  			"advanced" : 0,  			"needtime" : 1749689,  			"needfetch" : 376,  			"savestate" : 14662,  			"restorestate" : 14662,  			"iseof" : 1,  			"invalidates" : 0,  			"ncounted" : 1643722,  			"nskipped" : 0,  			"inputstage" : {  				"stage" : "fetch",  				"nreturned" : 1643722,  				"executiontimemillisestimate" : 80890,  				"works" : 1750065,  				"advanced" : 1643722,  				"needtime" : 105967,  				"needfetch" : 376,  				"savestate" : 14662,  				"restorestate" : 14662,  				"iseof" : 1,  				"invalidates" : 0,  				"docsexamined" : 1643722,  				"alreadyhasobj" : 0,  				"inputstage" : {  					"stage" : "ixscan",  					"nreturned" : 1643722,  					"executiontimemillisestimate" : 3800,  					"works" : 1749689,  					"advanced" : 1643722,  					"needtime" : 105967,  					"needfetch" : 0,  					"savestate" : 14662,  					"restorestate" : 14662,  					"iseof" : 1,  					"invalidates" : 0,  					"keypattern" : {  						"createdat" : -1,  						"status" : -1,  						"oid" : -1  					},  					"indexname" : "moderatedcontent",  					"ismultikey" : false,  					"direction" : "forward",  					"indexbounds" : {  						"createdat" : [  							"(new date(1467195213000), new date(1459246413000)]"  						],  						"status" : [  							"[\"undecided\", \"undecided\"]",  							"[\"approved\", \"approved\"]"  						],  						"oid" : [  							"[maxkey, minkey]"  						]  					},  					"keysexamined" : 1749689,  					"dupstested" : 0,  					"dupsdropped" : 0,  					"seeninvalidated" : 0,  					"matchtested" : 0  				}  			}  		},  		"allplansexecution" : [ ]  	}

for find.

"executionstats" : {  		"executionsuccess" : true,  		"nreturned" : 1643722,  		"executiontimemillis" : 1216,  		"totalkeysexamined" : 1749689,  		"totaldocsexamined" : 0,  		"executionstages" : {  			"stage" : "projection",  			"nreturned" : 1643722,  			"executiontimemillisestimate" : 1080,  			"works" : 1749690,  			"advanced" : 1643722,  			"needtime" : 105967,  			"needfetch" : 0,  			"savestate" : 13669,  			"restorestate" : 13669,  			"iseof" : 1,  			"invalidates" : 0,  			"transformby" : {  				"_id" : 0,  				"status" : 1  			},  			"inputstage" : {  				"stage" : "ixscan",  				"nreturned" : 1643722,  				"executiontimemillisestimate" : 920,  				"works" : 1749690,  				"advanced" : 1643722,  				"needtime" : 105967,  				"needfetch" : 0,  				"savestate" : 13669,  				"restorestate" : 13669,  				"iseof" : 1,  				"invalidates" : 0,  				"keypattern" : {  					"createdat" : -1,  					"status" : -1,  					"oid" : -1  				},  				"indexname" : "moderatedcontent",  				"ismultikey" : false,  				"direction" : "forward",  				"indexbounds" : {  					"createdat" : [  						"(new date(1467195213000), new date(1459246413000)]"  					],  					"status" : [  						"[\"undecided\", \"undecided\"]",  						"[\"approved\", \"approved\"]"  					],  					"oid" : [  						"[maxkey, minkey]"  					]  				},  				"keysexamined" : 1749689,  				"dupstested" : 0,  				"dupsdropped" : 0,  				"seeninvalidated" : 0,  				"matchtested" : 0  			}  		}  	}

for post .explain() first in cursor:

"executionstats" : {  		"executionsuccess" : true,  		"nreturned" : 0,  		"executiontimemillis" : 89191,  		"totalkeysexamined" : 1749689,  		"totaldocsexamined" : 1643722,  		"executionstages" : {  			"stage" : "count",  			"nreturned" : 0,  			"executiontimemillisestimate" : 83400,  			"works" : 1751709,  			"advanced" : 0,  			"needtime" : 1749689,  			"needfetch" : 2019,  			"savestate" : 15648,  			"restorestate" : 15648,  			"iseof" : 1,  			"invalidates" : 0,  			"ncounted" : 1643722,  			"nskipped" : 0,  			"inputstage" : {  				"stage" : "fetch",  				"nreturned" : 1643722,  				"executiontimemillisestimate" : 83260,  				"works" : 1751708,  				"advanced" : 1643722,  				"needtime" : 105967,  				"needfetch" : 2019,  				"savestate" : 15648,  				"restorestate" : 15648,  				"iseof" : 1,  				"invalidates" : 0,  				"docsexamined" : 1643722,  				"alreadyhasobj" : 0,  				"inputstage" : {  					"stage" : "ixscan",  					"nreturned" : 1643722,  					"executiontimemillisestimate" : 8290,  					"works" : 1749689,  					"advanced" : 1643722,  					"needtime" : 105967,  					"needfetch" : 0,  					"savestate" : 15648,  					"restorestate" : 15648,  					"iseof" : 1,  					"invalidates" : 0,  					"keypattern" : {  						"createdat" : -1,  						"status" : -1,  						"oid" : -1  					},  					"indexname" : "moderatedcontent",  					"ismultikey" : false,  					"direction" : "forward",  					"indexbounds" : {  						"createdat" : [  							"(new date(1467195213000), new date(1459246413000)]"  						],  						"status" : [  							"[\"undecided\", \"undecided\"]",  							"[\"approved\", \"approved\"]"  						],  						"oid" : [  							"[maxkey, minkey]"  						]  					},  					"keysexamined" : 1749689,  					"dupstested" : 0,  					"dupsdropped" : 0,  					"seeninvalidated" : 0,  					"matchtested" : 0  				}  			}  		}  	}

find - finds documents in collection , returns cursor selected documents. cursor pointer result set of query. clients can iterate through cursor retrieve results.

count - count() equivalent db.collection.find(query).count()

count cursor method, shell provides shortcut. in shell have shortcut count shown here db.collection.count(query)

reason: in find operation not iterating on result set, whereas in count operation doing operation on result set. cause time delay.

https://docs.mongodb.com/manual/reference/method/db.collection.find/ https://docs.mongodb.com/manual/reference/method/db.collection.count/


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -