質問

I have two fields a and b, where b has substantially higher selectivity than a.

Now, if I am only querying on both a and b (never on either field by itself), which of the following two indexes is better and why:

  1. {a: 1, b : 1}
  2. {b: 1, a : 1}

Explain seems to return almost identical results, but I read somewhere that you should put higher selectivity fields first. I don't know why that would make sense though.

役に立ちましたか?

解決 2

After doing some further analysis the two indexes are in fact pretty much identical from a performance point of view.

Really if you are in a similar situation, the real consideration should be whether in the future you might be more likely to query on a alone or b alone, and put that field first in the index.

他のヒント

After some extensive work to improve queries on a 150 000 000 records database I have found out the following:

not necessarily higher selectivity fields, but actually fields that are "faster" to match, being moved to the first position can increase performance drastically

I had an index composed of the following fields:

zip, address, city, first name, last name

Address is matched by an array, not string = string so it takes most time to execute and is the slowest to match. My first index that I created was: address_zip_city_last_name_first_name and the execution time for matching 1000 records against the whole DB would go for hours.

Address field actually probably has the highest selectivity on these, but since it is not being matched by a simple string equality, it takes the most time. It actually goes something like this

{ address: {$all : ["1233", "main", "avenue] }}

By changing this index to having the "faster" fields in the beginning, for example: zip_city_first_name_last_name_address the performance was much better. The same 1000 records would match in just one second instead for going for hours.

Hope this helps someone

cheers

I believe the optimiser will choose the index best to use, although you can provide hints

e.g.

db.collection.find({user:u, foo:d}).hint({user:1});

see http://www.mongodb.org/display/DOCS/Optimization

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top