1

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

1 Answer 1

3

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.