Thursday, November 24, 2011

MongoDB Indexes

To make the query fast we use indexes. Without indexes mongodb query optimizer need to check all the documents in order to sort and order the results.

Simplest form of creating an index.
> db.family.find();

{ "_id" : ObjectId("4ec0d4bc5c77613167cdf743"), "name" : "kasun" }
{ "_id" : ObjectId("4ec0d4bf5c77613167cdf744"), "name" : "kushan" }
 

> db.family.ensureIndex({name : 1});
> db.family.getIndexes();
[
        {
                "name" : "_id_",
                "ns" : "kasun.family",
                "key" : {
                        "_id" : 1
                },
                "v" : 0
        },
        {
                "_id" : ObjectId("4ec0db7f5c77613167cdf745"),
                "ns" : "kasun.family",
                "key" : {
                        "name" : 1
                },
                "name" : "name_1",
                "v" : 0
        }
]

ensureIndex()
Function create index based on the parameter pass within. It only create the index if it is not exists.

When we create index (standard) it block the other database operations. So the best way would be create the index with “background : true”. This will make sure the database is responsive even when you creating an index. When the collection data is very large this function is useful.
“name : 1” means the index is ascending order. You can give -1 also which is in descending order.
Even creating compound index is possible. You can use the ensureIndex() except it has multiple keys rather single key. And it is very important to choose either 1 or -1 for index in compound index (for single key it doesn’t matter). It will use this in sorts or range queries.

db.family.ensureIndex({name : 1, age: -1});
When you have multiple keys you can use it for beginning subset of a field.

_id index
Every collection has this type of index. It will automatically create during collection creation and can’t be deleted.

Anyway capped collections deviate from this behavior.
Dotted indexes

You can index a key inside embedded document. Also known as “Dot Notation”. Same time you can index a document as well.
db.places.insert( { name: "home", address: { city: "bandarawela", prov: "Uva", district: "badulla" } } );

db.places.ensureIndex( { address : 1 } );
// below queries can use the above index

db.places.find( { "address" : { "city" : "bandarawela", "prov" : "Uva", "district" : "badulla" } } );
// below one match, as { city: "bandarawela" } < { city: "bandarawela", prov: "Uva" }
db.places.find( { address: { $gte : { city: "bandarawela" } } } );

// below query does not match the document cause the order of fields is significant
db.factories.find( { address: { district: "badulla" ,prov : "Uva", city: "bandarawela" } } );

Sparse Index
The documents with indexed field, Documents which doesn’t have the sparse index will not be including into the index. These indexes are not complete. Behave differently on sorts and filtering because some documents not are including to the indexes.

Unique Index
A duplicate index field for this type of document is not allowed.

db.family.ensureIndex({name: 1}, {unique: true});
dropdups - This is fabulous feature in a way. Only limitation is it only keep the 1st document except others will be drop out.

This is feature you can use for existing collection when you want to create new index where there will be duplicates, and except for first document others will be drop.

More Index

Commonly used commands

To drop all the indexes can use the below method.
db.collection.dropIndexes();

To drop single key.
db.collection.dropIndexes({name :1});

To recreate index.
db.collection.reIndex();

No comments:

Post a Comment