Sebastian
Sebastian

Reputation: 4811

Create Multiple Index Or Create one Compound Index on MongoDb Collection

I got a Collection with following structre

_id : 641c2844904d1c6a6e8985a6
PortalId:123,
ContactId :1,
SelectionId:1,
SelectionName:"TEST",
SelectionCategory:"cat",
SelectionCategoryId:1,
DateCreated:2023-03-23T10:21:55.578+00:00
CreatedBy: "test"

I have to query against this collection based on various scenarios

Sometimes query against just 1 filter PortalId

Sometimes query against just 1 filter ContactId

Sometimes query against 1 filter SelectionId

Sometimes query against 2 filtes ContactId & SelectionId

Is it recommended to create a single compound index with columns ContactId & SelectionId and 1 index for PortalId or 3 individual index seperatly [ PortalId , ContactId & SelectionId ]

Will the the read performance affect much if we are creating just the compound index as the size of data grows?

Upvotes: 1

Views: 374

Answers (1)

Tarun Gaur
Tarun Gaur

Reputation: 81

Yes, you can create a single compound index with columns ContactId & SelectionId and 1 index for PortalId. This is because a compound index can be used to satisfy queries on multiple fields as well as single fields, while individual indexes can only be used to satisfy queries on a single field.

For read performance in large datasets, it is recommended to create a compound index instead of individual index as that will take up more space.

Lastly, is your application write intensive or read intensive as updating documents frequently will create load on your system with compound index.

Upvotes: 2

Related Questions