Nick Caruso
Nick Caruso

Reputation: 507

Mongo Index strategy for similar indexes

I have the following Schema:

{
  A: String,
  B: String,
  C: String,
  D: String,
}

If I have a where filter with A, B, C, and A, B, D, should I:

index (A, B, C) and (A, B, D)

OR

index (A, B), and separately (C) and (D)?

OR

index (A, B, C, D)?

Upvotes: 0

Views: 17

Answers (1)

Alex Blex
Alex Blex

Reputation: 37038

index (A, B, C) and (A, B, D)


index (A, B), and separately (C) and (D) requires index intersection which has so many conditions to meet that it almost never happens in the wild.

index (A, B, C, D) will work perfectly for (A, B, C) queries and only as good as index (A, B) alone for (A, B, D) queries.

It's very well documented in https://docs.mongodb.com/manual/core/index-compound/#prefixes

Upvotes: 2

Related Questions