Create indexes in production

The chapter 5 of M001 discusses about indexes as a way to make queries efficient and much faster. However mongoose - the nodejs most popular mongodb driver - advises against creating indexes in production here: https://mongoosejs.com/docs/guide.html#autoIndex

I wonder if they are talking about the same thing or something different

imho autoIndex creates an index for every field, and maybe every subfield of the Schema.

They recommend is to set autoIndex to false; but using indexes for frequently run queries is a good practice.

Now, why not to create indexes for every field?

In principle, there are 2 reasons:

  • Indexes can become large storage-wise
    And more importantly
  • indexes are held in ram memory

You don’t want anything that is not very frequently used to be in ram memory.

The link you provided does not advises against creating indexes in production. It advises not to use autoIndex. Principally because:

While nice for development, it is recommended this behavior be disabled in production since index creation can cause a significant performance impact.

I embolden the critical part of the sentence. It is the index creation that has a significant negative impact. Having index is really like they discuss in the course.

Also note, that autoIndex does not mean that you create an index for every field. Instead, it means:

Mongoose automatically calls createIndex for each defined index in your schema

An extra bad thing with autoIndex is that if you change the index definition in your schema and the index already exist in MongoDB, an error will be thrown.

Okay, if I understand correctly, with the link I shared it is the autoindex that should be deactivated in production.

Actually what brought me there (on the autoindex page) was this comment here: https://github.com/panva/node-oidc-provider/blob/master/example/adapters/mongodb.js#L51 as I was looking for an official reference of that comment.

I am trying to use that adapter in production but I am not sure to understand what the contributor meant by not creating the indexes in production. Or the other way, is there a safe way to make sure that those indexes are created without impacting the performance ?

I like to have an update-database script, that drops deprecated collections, creates new collections and indexes before hand. The application then just assumes everything is there. I used the same script for development, testing and then production. It makes everything consistent.

Is the issue in the link I shared related to duplicated indexes @steevej-1495 ?

Hi @edkeveked_N_A,

I would recommend you to ask this question in our community forum under drivers category.