Understanding Why Count on MongoDB View is So Slow


I am trying to understand why running a count on my MongoDB view takes so long to perform, and if there are things I can do to speed it up. It looks like this:



I am passing filter params in as the param "search" within find(), and then running the count.



The target collection of this view has about a half million docs, though the view matches to only about 8,000 of those. In the view I am matching on an indexed field from the targeted collection in stage 1 of my aggregation pipeline - that's what takes me down to 8,000 or so documents.



Now, because the count on the much larger target collection uses meta data, it comes back really quickly - in about 240ms. But the count on the view takes over 6 seconds to return, on, as I say, a much smaller data set. Even with the find(search) segment taken out, it still takes over 6 seconds for the count to complete.



By the way, I also tried:



... with the same result. And according to the docs, db.orders.count() is equivalent to db.orders.find().count().



Why is this count on the view so slow? Is this common? And what can I do to speed it up?



EDIT: Someone mentioned creating a separate collection for this, rather than using a view. That occurred to me as well. Is this a good option when you need to run a count? I'm trying to wrap my head around the costs vs. benefits of choosing a view or a collection in this kind of situation.



By the way, running db.view_accounts_report.find({}).explain('executionStats') results in the following:






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

The Dalles, Oregon

眉山市

清晰法令