T
T
tac2012-05-30 14:57:47
MongoDB
tac, 2012-05-30 14:57:47

Does Mongo select queries optimize?

Let's say there are two SQL queries

select * from users
and
select username, email from users

As I understand it, they correspond to

> db.users.find();
and
> db.users.find( { }, { username: 1, email: 1 } );

Question: Does Mongo optimize these queries? Does he have a time difference for these requests?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
CrazySquirrel, 2012-05-30
@CrazySquirrel

Full sample

{
  "cursor" : "BasicCursor",
  "nscanned" : 1136854,
  "nscannedObjects" : 1136854,
  "n" : 1136854,
  "millis" : 45205,
  "nYields" : 0,
  "nChunkSkips" : 0,
  "isMultiKey" : false,
  "indexOnly" : false,
  "indexBounds" : {
    
  }
}


Selecting one field
{
  "cursor" : "BasicCursor",
  "nscanned" : 1136854,
  "nscannedObjects" : 1136854,
  "n" : 1136854,
  "millis" : 40294,
  "nYields" : 0,
  "nChunkSkips" : 0,
  "isMultiKey" : false,
  "indexOnly" : false,
  "indexBounds" : {
    
  }
}

The real profit will be if only the fields in the index are selected.

A
Anatoly, 2012-05-30
@taliban

you can tritely execute a bunch of such requests and detect the time =) you will visually see the difference, at the same time tell us here.

E
egorinsk, 2012-05-30
@egorinsk

If you only need 2 fields, then the second option is better, since in the first one you will send the entire collection to the client.
I don’t think that mongo optimizes anything, since there is nothing to optimize here - in both cases, you need to bypass the entire collection, just in the second case, the amount of data sent is less.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question