simolus3 / drift

Drift is an easy to use, reactive, typesafe persistence library for Dart & Flutter.
https://drift.simonbinder.eu/
MIT License
2.63k stars 369 forks source link

Drift web indexedDb slow #2657

Open Kiranafast2023 opened 1 year ago

Kiranafast2023 commented 1 year ago

I am using the latest flutter and drift version in my project. When running on web (indexedDb) , it takes a lot of time to fetch the data from databse. IDB has onlt few entries in a table but each row has 6k of data in which one column consists of ~5k data in JSON text format,

This is how I have initialled the DB - final db = Database( WebDatabase.withStorage( DriftWebStorage.indexedDb('worker', migrateFromLocalStorage: false, inWebWorker: true), ), ); db.customStatement('PRAGMA journal_mode=WAL'); Not sure why its so slow on web, on andorid and iOS it works fine

simolus3 commented 1 year ago

db.customStatement('PRAGMA journal_mode=WAL');

This isn't the reason, but WAL has no effect on the web.

IDB has onlt few entries in a table but each row has 6k of data in which one column consists of ~5k data in JSON text format,

Yes, drift is storing the database in 4k chunks. We're not reading that as JSON though.

What exactly is slow? Writes to the database or reads as well? Reads should be fairly fast because we keep the entire database in memory with IndexedDB. But writes in package:drift/web.dart are very slow and that is unfixable. You could also the new web implementation which uses a more sound implementation as well.

Kiranafast2023 commented 1 year ago

Thanks for the response, as I can see after adding the new way of implementation code, database transactions are faster now. But on web, when I refresh the page, the database becomes empty. Any idea how to solve this? This was not happening before. This is the code -

DatabaseConnection connectOnWeb() {
  return DatabaseConnection.delayed(Future(() async {
    final result = await WasmDatabase.open(
      databaseName:
          'myDb', // prefer to only use valid identifiers here
      sqlite3Uri: Uri.parse('sqlite3.wasm'),
      driftWorkerUri: Uri.parse('drift_worker.dart.js'),
    );

    if (result.missingFeatures.isNotEmpty) {
      debugPrint('Using ${result.chosenImplementation} due to missing browser '
          'features: ${result.missingFeatures}');
    }

    return result.resolvedExecutor;
  }));
}
simolus3 commented 1 year ago

Interesting, I haven't seen that before. Do you see a message about it using an in-memory implementation due to missing browser features? Or, which implementation is mentioned in the debugPrint message?

Kiranafast2023 commented 1 year ago

So its not going in the IF condition. I am using latest chrome and if I remove the if condition and try to run debugPrint directly - this is the output it shows Using WasmStorageImplementation.inMemory due to missing browser features: {}

simolus3 commented 1 year ago

That sounds like something is going very wrong, is there any error before that? Do you have the drift_worker.dart.js file in web/? That message can appear when drift was unable to load the worker.

Mubark-p commented 1 year ago

change this line driftWorkerUri: Uri.parse('drift_worker.dart.js'), to driftWorkerUri: Uri.parse('drift_worker.js'),

ensure to add drift_worker.js to web folder