Home:ALL Converter>MongoDB large collection slow search

MongoDB large collection slow search

Ask Time:2013-05-13T18:17:39         Author:sashab

Json Formatter

I have large mongodb collection(5.3 million entries), each entry has list-field and some additional fields. For example:

{ "_id" : ObjectId("518d51c808beda0b70cffffa"), 
  "a" : [ 0.00037, 0.00009 ], 
  "b" : "Some long str", 
  "c" : [ "element1", "element2", "element3" ] 
}

I have index on field c and I want to make search on it. Moreover I want to search by all permutations of this list, for example I want object above to be in search result for query "c": ["element3", "element2", "element1"].

I use pymongo this way:

from itertools import permutations
...
query = ['element1', 'element2', 'element3']
query_permutations = list(permutations(query, len(query)))
results = collection.find({"c": {"$in": query_permutations}}).sort("a", -1)

Is there any way to make it faster?

UPD: explain() on smaller version of collection:

{
    "cursor" : "BasicCursor",
    "isMultiKey" : false,
    "n" : 11053,
    "nscannedObjects" : 11053,
    "nscanned" : 11053,
    "nscannedObjectsAllPlans" : 11053,
    "nscannedAllPlans" : 11053,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "millis" : 41,
    "indexBounds" : {

    },
    "server" : "machine.local:27017"
}

Author:sashab,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/16519643/mongodb-large-collection-slow-search
yy