Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why was the explain query output giving me BasicCursor eventhough the collection had indexes on it?

Tags:

mongodb

I have a collection named stocks , i created a compound index on it as shown below

db.stocks.ensureIndex({"symbol":1,"date":1,"type": 1, "isValid": 1,"rootsymbol":1,"price":1},{"unique" : false})

I have set profilinglevel , to find out all the slow queries .

One of the query was below took 38 millis , when did explain on it , this was the below result

Sorry i have updated my question

db.stocks.find({ query: { symbol: "AAPLE", date: "2014-01-18", type: "O", isValid: true }, orderby: { price: "1" } }).explain();
{
        "cursor" : "BasicCursor",
        "nscanned" : 705402,
        "nscannedObjects" : 705402,
        "n" : 0,
        "millis" : 3456,
        "indexBounds" : {

        }
}

My question is why its showing a BasicCursor even though it had indexes on it ??

like image 319
Pawan Avatar asked Dec 11 '25 05:12

Pawan


2 Answers

I'm pretty sure that the issue here is your use of the find() function. You are specifying a query parameter and inside it, placing your search criteria. I don't think that you need to actually put query in there. Simply insert your search criteria. Something like this:

db.stocks.find({ 
  symbol: "AAPLE", 
  date: "2014-01-18", 
  type: "O", 
  isValid: true 
}).sort( { "price": 1} ).explain();

Note also my changes to the sorting. You can read more about sorting a cursor here.

like image 129
Lix Avatar answered Dec 14 '25 03:12

Lix


Since the problem isn't actually described I will go on to describe it.

You are calling top level query operators with functional operators. So for example you call query operators here:

{ query: { symbol: "AAPLE", date: "2014-01-18", type: "O", isValid: true }, orderby: { price: "1" } }

In the form of query and orderby but then you call a functional operator:

explain();

This is a known bug with MongoDB that these two do not play well together and so produce the output you get.

Of course when the query comes in and is parsed by MongoDB it is recorded in the profile with query operators query and orderby and maxscan etc.

This is more of a problem when calling the command.

Reference: MongoDB $query operator ignores index? I couldn't find the actual JIRA for this but this is related.

Edit: I think this vaguely represents it: https://jira.mongodb.org/browse/SERVER-6767

like image 41
Sammaye Avatar answered Dec 14 '25 02:12

Sammaye



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!