Reputation: 5369
If I have a collection which I query based on fields _id and CreationTimestamp.O as below:
var _query = Query.And(
Query.EQ("_id", "TheId"),
Query.GT("CreationTimestamp.0", DateTimeOffset.UtcNow.AddMinutes(-15).UtcTicks));
Should I create a compound index on both these fields? I know _id by default has an index.
I am looking for guidance on best practice i.e. Create a compound index for both fields or just create an index for CreationTimestamp.O as their is already an index on _id
Upvotes: 1
Views: 360
Reputation: 12510
There's not much benefit in creating compound index here. Since the selectivity of autocreated index on _id
field is 1 (it's unique index) there's no reason in having any compound index prefixed with it. The only possible benefit may be the fact MongoDB can use covered index for this query and possibly speedup the things (you need to benchmark on your data to prove this).
Upvotes: 3