Monday, 26 August 2013

Is Compound Index on a collection is always better than having single Indexes

Is Compound Index on a collection is always better than having single Indexes

I have come across this particular line with respect to indexes in mongdb .
Each additional index on a collections does impose some overhead when
performing inserts or updates that involve changing index entries.
which means that with respect to my understanding , if there are more
number of indexes on a collection it will degrade performance during
insertion or Updation .
So is it always true that compound indexes are always better than single
indexes ??
For example if i have a collection named stocks
and a compound index is present on it as shown below
db.stocks.ensureIndex({"symbol":1,"date":1,"type": 1,"price":1},{"unique"
: false})
and the above is better than the individual indexes shown below .
db.stocks.ensureIndex({"symbol" : 1}, {"unique" : false})
db.stocks.ensureIndex({"date" : 1}, {"unique" : false})
db.stocks.ensureIndex({"type" : 1}, {"unique" : false})
db.stocks.ensureIndex({"price" : 1}, {"unique" : false})
Please let me know if i am correct or not ??

No comments:

Post a Comment