A
A
Andrey2019-08-05 11:01:44
MongoDB
Andrey, 2019-08-05 11:01:44

What is the best way to index a collection?

Good day!
There is a collection with approximately the following document structure:

{
  "alias": "a",
  "a_key": 10,
  "b_key": 15
},{
  "alias": "a",
  "a_key": 12,
  "b_key": 14
},{
  "alias": "b",
  "a_key": 8,
  "b_key": 17
}

collection selections will always be in this format:
alias="a" and ( (a_key between 10 and 15) or (b_key between 10 and 15))
in this case it would be better to make 3 indexes:
alias
a_key
b_key
or 2 indexes:
alias+a_key
alias+b_key?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey, 2019-08-07
@VladimirAndreev

I will answer myself.
Mongodb seems to use only one index when fetching, so
alias+a_key+b_key was the most efficient

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question