Best index creation strategy

Please find the schema of a mongodb document.

{ 
	"_id" : ObjectId("000ac1da28d48426a46ba80b"), 
	"sentDate" : ISODate("2022-05-10T19:49:00Z"), 
	"rootId" : UUID("1623178c-9a7b-4e4f-a49c-6dda5cc4f47f"), 
	"vehicleId" : UUID("ff4fc2e9-9dae-4cb5-8834-c654fc39faa6"),
	"serialNumber" : "AB00016987"
	"points" : [{ 
		"point" : { 
			"type" : "Point", 
			"coordinates" : [82.655769000000006, -17.650956999999998] 
		}
	}] 
}

A collection has few billions documents. We have to create an index to improve the read operation and have to be careful for write operation as well.

db.sampleData.createIndex( { rootId: 1,  vehicleId: 1, sentDate: -1 } );
OR
db.sampleData.createIndex( { rootId: 1,  serialNumber: 1, sentDate: -1 } );

Which index would be the best per MongoDB? Any suggestion on this.

Thanks much!

Hi @AARZOO_MANOOSI ,

Which index is better depanding on type of queries and their operators … Just by looking on a sample documents we can never know.

Depanding on your server version , you may consider different strategies. Prior to 4.4 the main recommendation was Building massive indexes in MongoDB is best done in a rolling. Manner over replica sets:

But post 4.4 it depends on how impactful is the optimized index build. Read more here:

Ty

1 Like

Sorry got busy and couldn’t visit this thread. We are using M50 cluster and sharded as well.

Hi @AARZOO_MANOOSI ,

Which index to create is based on your queries that you need, perhaps you need booth if you use different queries.

For atlas sharded enviroments we recommend using the rolling index builds.

Thanks
Pavel