Search code examples
androidionic-frameworkpouchdbrelational-pouch

Difference between allDocs, find performance in relational-pouch


I'm developing an Ionic App. I use PouchDB with relational-pouch localy and Cloudant remote. I run into several performance issues. Currently there are only 200 docs in the db. The syncing works fine.

When I run this code after the initial sync has been completed, I get, in my eyes, strange results. allDocs finishes fast after 60ms. find after 180ms. I use find here from pouchdb not relational-pouch. This is intenet. I created an Index for the field data.type. Is this the expected behaviour? I expect the view to be much faster as basically everything should be preloaded/ faster with the index. Maybe I didn't do the Index correct. Please advice.

Test script

 console.time("allDocs");
        this.dataService.db.allDocs({
          include_docs: true,
          startkey: "supplier_2",
          endkey: "supplier_2\uffff"
        }).then(articles=>{  
          console.timeEnd("allDocs");
          console.log(articles);

            console.time("find");
            this.dataService.db.find({
              selector: {'data.type': 'supplier'},
            }).then(suppliers=> {
              console.timeEnd("find");
              console.log(suppliers);
            }).catch(err=> {
             console.log(err);
            });
          });

Index

this.db.createIndex({
      index: {
        fields: ['data.type'],
        name: 'dataTypeIndex',
        ddoc: 'dataType'
      }
    })

Thanks for your support! :)


Solution

  • The fastest way to query data is using primary indexes (the documents _id). When you're using the allDocs with a startKey and an endKey you're basically using the primary index to search for data. If you use smart ids like ${doc.type}::${someId} then searching by type should be faster when using allDocs.

    On the other hand, find queries (or mango queries) use secondary indexes that are built with createIndex. Use them in cases where you can't perform a query based on the id of a document. These are generally slower and have some drawbacks like you can't use regex to search by some text in a field.

    This article might help you (especially the part "Use and abuse your doc IDs" )

    https://pouchdb.com/2014/05/01/secondary-indexes-have-landed-in-pouchdb.html