0

For optimal performance, if you provide both a sort and a query to the MapReduce, should you have:

  1. one index with fields used in the sort, then the fields used in the query

  2. one index with fields used in the query, then the fields used in the sort

  3. two separate indexes


E.g. document contains fields A, B, C, D. Map-Reduce is using a sort on field A and a query by fields B, C.

Which of the following indexes would be preferable:

  1. { "A" : 1, "B" : 1, "C" : 1 }

  2. { "B" : 1, "C" : 1, "A" : 1 }

  3. { "A" : 1 }, { "B" : 1, "C" : 1 }


Is this documented anywhere? (Index usage by map-reduce when using both sort and query.)

Danny Varod
  • 17,324
  • 5
  • 69
  • 111
  • 1
    Could you show us an attempt? Or at the very least some source data and an expected result please? – Neil Lunn Mar 19 '15 at 13:04
  • @NeilLunn The question relates to how MongoDB works. It isn't specific to a single case. I know how to use the map-reduce and how to create indexes, however, given the time it takes for the indexes to be generated for large collection, I'd rather create the right ones in advance and not guess and then check if it was the right index. – Danny Varod Mar 19 '15 at 13:22
  • 2 is best and works very well assuming the conditions on `B` and `C` are equality conditions. You can test this easily in the shell - just try `db.collection.find(query).sort(sort).explain()` with various indexes. See also [Sort Results with Indexes](http://docs.mongodb.org/manual/tutorial/sort-results-with-indexes/) from the manual. – wdberkeley Mar 19 '15 at 18:31
  • @wdberkeley If you add this as an answer (with this link), I'll select it. – Danny Varod May 07 '15 at 10:21

0 Answers0