Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB : Indexes order and query order must match?

Tags:

mongodb

This question concern the internal method to manage indexes and serching Bson Documents.

When you create a multiple indexes like "index1", "index2", "index3"...the index are stored to be used during queries, but what about the order of queries and the performance resulting.

sample
index1,index2,index3----> query in the same order index1,index2,index3 (best case) index1,index2,index3----> query in another order index2,index1,index3 (the order altered)

Many times you use nested queries including these 3 index and others items or more indexes. The order of the queries would implicate some time lost?. Must passing the queries respecting the indexes order defined or the internal architecture take care about this order search? I searching to know if i do take care about this or can make my queries in freedom manier.

Thanks.

like image 613
user325558 Avatar asked Mar 09 '11 12:03

user325558


People also ask

Does order of index matter in MongoDB?

The order of the fields in the index only matters if the query doesn't include all of the fields in the index. This query is referencing all three fields so the order of the fields in the index doesn't matter. See more details in the docs on compound indexes.

Can a query use multiple indexes MongoDB?

MongoDB can use the intersection of multiple indexes to fulfill queries. In general, each index intersection involves two indexes; however, MongoDB can employ multiple/nested index intersections to resolve a query.

How indexes affect sorting in MongoDB?

Since indexes contain ordered records, MongoDB can obtain the results of a sort from an index that includes the sort fields. MongoDB may use multiple indexes to support a sort operation if the sort uses the same indexes as the query predicate.

Does indexing improve query performance MongoDB?

Performance. Because the index contains all fields required by the query, MongoDB can both match the query conditions and return the results using only the index. Querying only the index can be much faster than querying documents outside of the index.


2 Answers

The order of the conditions in your query does not affect whether it can use an index or no.

e.g. typical document structure:

{
    "FieldA" : "A",
    "FieldB" : "B"
}

If you have an compound index on A and B :

db.MyCollection.ensureIndex({FieldA : 1, FieldB : 1})

Then both of the following queries will be able to use that index:

db.MyCollection.find({FieldA : "A", FieldB : "B"})
db.MyCollection.find({FieldB : "B", FieldA : "A"})

So the ordering of the conditions in the query do not prevent the index being used - which I think is the question you are asking.

You can easily test this out by trying the 2 queries in the shell and adding .explain() after the find. I just did this to confirm, and they both showed that the compound index was used.

however, if you run the following query, this will NOT use the index as FieldA is not being queried on:

db.MyCollection.find({FieldB : "B"})

So it's the ordering of the fields in the index that defines whether it can be used by a query and not the ordering of the fields in the query itself (this was what Lucas was referring to).

like image 182
AdaTheDev Avatar answered Oct 20 '22 15:10

AdaTheDev


From http://www.mongodb.org/display/DOCS/Indexes:

If you have a compound index on multiple fields, you can use it to query on the beginning subset of fields. So if you have an index on

a,b,c

you can use it query on

a

a,b

a,b,c

So yes, order matters. You should clarify your question a bit if you need a more precise answer.

like image 27
Lucas Zamboulis Avatar answered Oct 20 '22 16:10

Lucas Zamboulis