Anukool
Anukool

Reputation: 812

why is indexing applied to a specific column and not on every column by default?

I have read about indexing in DB, the point i don't understand is that why do we need to specify index to a particular column, if it makes the search fast, why it is not by default to all columns? is it possible to create multiple indexes in one table?

Upvotes: 1

Views: 355

Answers (2)

matthPen
matthPen

Reputation: 4363

To resume :

Using indexes improves performances on read query, because mongodb doesn't read entire collection when searching documents. It also improves sorting.

But these improvments have a cost :

  • Indexes use disk/memory space.
  • Delete, insert and update operations will be longer : on each insert, delete or update operation, mongodb must update all you concerned indexes.
  • There are multiple indexes type, and some of them (compound index ie) can have planty of combinations

For these reasons (but not only), by default only index on _id field (as it need to be unique) is created on collection creation.

Upvotes: 2

Ankit
Ankit

Reputation: 990

If there are n no. of indices and when you perform any save/update operation that modifies keys, it does it n times, hence produces an excessive write lock. When you will perform such operation, you will observe that reads would be faster with no indexes than when trying to update consistently with too many indexes. So in order to perform indexing, you should keep track of indexes else there would be a great performance issue (sake of RAM and write lock).

Upvotes: 1

Related Questions