1

I was wondering whether someone could provide some advice on my cloudant query below. It is now taking upwards of 20 seconds to execute against a DB of 50,000 documents - I suspect I could be getting better speed than this.

The purpose of the query is to find all of my documents with the attribute "searchCode" equalling a specific value plus a further list of specific IDs.

Both searchCode and _id are indexed - any ideas why my query would be taking so long / what I could do to speed it up?

mydb.find({selector: {"$or":[{"searchCode": searchCode},{"_id":{"$in":idList}}]}}, function (err, result) {
   if(!err){
      fulfill(result.docs);
   }
   else{
      console.error(err);
   }
});

Thanks, James

J Deane
  • 47
  • 5

1 Answers1

1

You could try doing separate calls for the queries

  • find me documents where the searchCode = 'some value'
  • find me documents whose ids match a list of ids

The first can be achieved with a find call and a query like so:

{ selector: {"searchCode": searchCode} }

The second can be achieved by hitting the databases's _all_docs endpoint, passing in the list of ids as a keys parameter e.g.

GET /db/_all_docs?keys=["a","b","c"]

You might find that running both requests in parallel and merging the results gives you better performance.

Glynn Bird
  • 5,507
  • 2
  • 12
  • 21
  • Thanks Glynn - I have separated the two and it seems the problem I am having is with with ID part of the call - the query by searchcode returns almost immediately but the ID call is still taking over 15 seconds - ill investigate this further – J Deane Dec 15 '16 at 13:49
  • Hi Glynn - I actually got this to work as you suggest using my modules .list method which must call GET/db/_all_docs however I'm now having an issue where the number of keys im passing in is exceeding the limit as a query parameter. Any ideas here? I've seen documentation suggesting you can retrieve all_docs with a POST on couch_db to submit a large quantity of keys - is this the same on cloudant? – J Deane Dec 15 '16 at 15:17
  • I resolved this - thanks you were right running them as separate queries were much more efficient. I did end up following my suggesting of POSTing to /db/_all_docs with the keys in the body which worked for my large set of keys – J Deane Dec 15 '16 at 16:15