User1230321
User1230321

Reputation: 1475

How to increase performance of pouchdb-find queries?

I'm using pouchDB replication from couchDB in react native. This is my package.json:

"pouchdb": "^6.3.4",
"pouchdb-find": "^6.3.4",
"pouchdb-react-native": "^6.3.4",

I've a database named "databaseA" where one of the column is "col1". I've written a query using pouchdb-find to get all records of databaseA with matching value in col1.

const localDB = new PouchDB('databaseA');
const remoteDB = new PouchDB('databaseA');
PouchDB.plugin(findPlugin);

localDB.replicate.from(
  remoteDB,
  (err, response) => {
    if (err) {
      return console.log(err);
    }
  },
);

localDB.createIndex({
  index: {
    fields: ['col1', 'col2'],
    ddoc: 'col1-col2-index',
  },
});

localDB.find({
  selector: {
    col1: {
      $eq: '1',
    },
    col2: true,
  },
  fields: ['_id', 'col1', 'col2' ],
  limit: 0,
  // sort: [‘_id’],
  use_index: 'col1-col2-index',
}).then((results) => {
  alert(`Index called to increase speed: ${results.docs.length}`);
}).catch((ex) => {
  // alert(`Index called to increase speed: Exception: ${JSON.stringify(ex)}`);
});

export function getItems(col1) {
  const startTime = new Date();
  return (dispatch, getState) => {
    return localDB.find({
      selector: {
        col1: {
          $eq: col1,
        },
        col2: true,
      },
      fields: ['_id', 'col2', 'col1' ],
      // sort: [‘_id’],
      use_index: 'col1-col2-index',
    }).then((results) => {
      const time = new Date().getTime() - startTime;
      alert(`Total time ${time}`);
    }).catch((ex) => {
      console.log(ex);
    });
  };
}

I've around 900 records in the database. It takes nearly 2minutes which is quite high to query. How can the performance of this query be increased? Please help

Upvotes: 3

Views: 897

Answers (0)

Related Questions