Skip to content

Performance issue on local database query (~11s for ~2k docs) #51

@Pierre-AlexandreTavel

Description

@Pierre-AlexandreTavel

🐌 Performance issue on local database query (~11s for ~2k docs)

Context

I'm using the pouchdb-adapter-react-native-sqlite adapter with React Native to query PouchDB database that has been replicate from a remote CouchDB database.

I was previously using pouchdb-adapter-react-native-sqlite with react-native-quick-websql and pouchDB 8
I followed the following doc to upgrade everything because I switched to the new arch of react native: https://www.devas.life/the-fastest-way-to-use-pouchdb-on-react-native-0-73/

Setup

  • React Native: 0.76.9
  • Expo: 52.0.46
  • Hermes: Enabled
  • New React Native architecture: Enabled
  • Adapter: react-native-sqlite via this library
  • Doc count: ~2162 documents

Problem

After replicating the remote DB locally, querying all documents with:

localDB.allDocs({ include_docs: true });

takes ~11 seconds, which seems excessively slow for such a small number of documents.

With a previous implementation (react-native-quick-websql) not using this adapter, the same query was significantly faster (almost instant).


Additional insight

  • I tried resetting the remote database, re-uploading the same 2k documents, then replicating again.
    → In that case, the query time dropped to under 1 second.
  • I also tried running compact and other maintenance operations on the original remote database,
    → but that did not help improve query performance after replication.

Reproduction code

import HttpPouch from "pouchdb-adapter-http";
import sqliteAdapter from "pouchdb-adapter-react-native-sqlite";
import PouchDB from "pouchdb-core";
import find from "pouchdb-find";
import mapreduce from "pouchdb-mapreduce";
import replication from "pouchdb-replication";
import pouchdbDebug from "pouchdb-debug";

export const loadPouchDBDatabases = async () => {
  let prefix = "";

  const PouchDBCore = PouchDB.plugin(HttpPouch)
    .plugin(replication)
    .plugin(mapreduce)
    .plugin(find)
    .plugin(sqliteAdapter)
    .plugin(pouchdbDebug);

  PouchDBCore.debug.enable("*");

  const user = {
    urlPlayDB: "xxxx",
    idPlayDB: "repdemoex",
    keyPlayDB:
      "VyCbpewmaiGsFgwloWvBOD0t0811fFSV0FN2Y40Lcy0ujw8IzfrtVwdzsEECdnGi",
    partner: "/partner/partners/30",
  };

  const remoteDb = new PouchDBCore(
    user.urlPlayDB + "/" + `${user.idPlayDB}_assortments_1-4-0`,
    {
      auth: {
        username: user.idPlayDB,
        password: user.keyPlayDB,
      },
    }
  );
  const localDB = new PouchDBCore(
    `${prefix}${user.idPlayDB}_assortments_1-4-0` + ".db",
    {
      adapter: "react-native-sqlite",
    }
  );

  await localDB.replicate.from(remoteDb);

  const localDBInfo = await localDB?.info();
  const remoteDBInfo = await remoteDb?.info();
  console.log("assortment", {
    local: localDBInfo?.doc_count,
    remote: remoteDBInfo?.doc_count,
  });

  localDB.allDocs({
    include_docs: true,
  }); // <-- ~11s delay here
};

Expected behavior

Querying all documents from the local database should return results within a reasonable time (<1s), even for 2000+ documents.


Questions

  • Is this performance expected with this adapter?
  • Could the performance degradation be linked to revision metadata or deletion tombstones in the replicated database?
  • Is there any SQLite tuning or adapter configuration that could help?
  • Any suggestions on how to investigate or mitigate this further?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions