Pergunta

After playing with

db.largecollection.find( { $or : [ { identifierX : "sha1_hash123" } , { identifierY : "md5_hash456" } , { identifierZ : "another_hash789" } ] } )

I checked the indexes that mongodb prepared automatically. in addition to the "single" ensureIndex for the identifiers x/y/z, there is a identifierX_1_identifierY_1_identifierZ_1 now and performance is down :-(

Do you have an idea or tip how to explain to mongodb that it's faster to use the indexes for the single identifiers because i do not have $and, but $or queries?

Thx

Foi útil?

Solução

MongoDB doesn't create indexes on its own. It's something that an application, user, or framework does. For your query, MongoDB could only use an index for either of identifierX, identifierY or identifierZ. However, if you don't have such an index then of course none will be used. The identifierX_1_identifierY_1_identifierZ_1 index can not be used for this query.

In this case, you will probably need to make an index for all of this identifiers:

db.ensureIndex( { 'identifierX' : 1 } );
db.ensureIndex( { 'identifierY' : 1 } );
db.ensureIndex( { 'identifierZ' : 1 } );

MongoDB can only use one index at a time, and it will try to pick the "best" one. Try using explain to see which indexed is being picked:

db.largecollection.find( { $or : [
    { identifierX : "sha1_hash123" },
    { identifierY : "md5_hash456" },
    { identifierZ : "another_hash789" }
] } ).explain();

That should give you some ideas on which index is being used.

There is an exception for $or though, where MongoDB can use a different index for each of the parts and de-dup for you. It's here in the docs. It would (of course) still not use the compound index, and you need the indexes that I've written here above.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top