1

Recently I have created a collection that contains a limited number (<5 by now) of kind of data, I created the collection with entity_type and entity_id, as following shown

+-------------+-----------+---------+------
| entity_type | entity_id | date    | ...
+-------------+-----------+---------+------

And since the query for the latest records are more frequency. So I created the following compound index (note that the entity_id is not globally unique)

[('date': -1), ('entity_type': 1), ('entity_id': 1)]

However, I am not sure if such index is okay since someone said that the index should not created on a field (here it is entity_type) which only has limited values.

1
  • Since your index includes other fields as well, which look like they will have a high cardinality (many possible values), I think this compound index will be fine. Commented May 26, 2016 at 5:30

1 Answer 1

1

It's recommended to create mongodb index on fields which ensures high selectivity.

So if entity_type column contains limited values, I would suggest to remove this column from compound index.

MongoDB Index Selectivity

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

1 Comment

The link you given said A better strategy, depending on your queries, would be to create a compound index that includes the low-selectivity field and another field. So the above index is also okay even with entity_type included.

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.