This is a slightly weird one.
I’m iterating through quite a large database, using a query that checks if certain elements exist. running this query in database without hint takes about 137 seconds, but with hint it goes down to 0 (which is obv preferable). When using it via php
$collection->find($params)->hint(array("responses" => 1))->count();
and profiling the queries I’m getting this:
{ “ts” : ISODate(“2012-05-08T10:11:24.682Z”), “op” : “command”, “ns” :
“ey_gsat.$cmd”, “command” : { “count” : “survey_answers”, “query” : {
“responses.93” : { “$exists” : true }, “responses.96” : { “$exists” :
true }, “responses.99” : { “$exists” : true }, “responses.102” : {
“$exists” : true }, “responses.105” : { “$exists” : true },
“responses.108” : { “$exists” : true }, “responses.111” : { “$exists”
: true }, “responses.114” : { “$exists” : true }, “responses.117” : {
“$exists” : true }, “responses.120” : { “$exists” : true },
“responses.1” : { “$exists” : true } } }, “ntoreturn” : 1,
“responseLength” : 48, “millis” : 137, “client” : “127.0.0.1”, “user”
: “” }
There is no sign of the query actually using an index when run via php. I have quite a few queries like this running on this site, so the 137 milliseconds all add up, hence the reason why I would like to optimise them. any ideas what might be wrong?
This is a known limitation of count: https://jira.mongodb.org/browse/SERVER-2677. You might want to follow/vote for the issue. Unfortunately, there’s no workaround. You might want to look into caching the count whenever possible or estimating the total.