Reputation: 75
I'm using Dexie DB version 2.0.4 with Angular 8 and electron. I'm realizing that the more data I input in the DB and the more queries I make to it the slower my DB calls become.
I'm a Dexie noob and unsure if there is a way to close connections after I make certain requests or if there is something getting stuck in the memory.
Anyone has any idea how to debug or confirm what can the issue be ?
Here is an example of one of the requests I make to the DB:
await this.indexdb.TableName.filter(SomeProperty => SomeProperty == Property).toArray();
await this.indexdb.TableName.put({'SomePropertyName':'SomePropertyValue'})
Thanks a lot
Upvotes: 2
Views: 1978
Reputation: 5691
The filter method does not utilize any index but uses a cursor to go through all rows in the table to test your criteria. This can become slow and especially slow when using Zone.js (which is a part of Angular) as it will intercept all IndexedDB events and do change detection for your entire app for each row that is being tested against your filter criteria.
You would get much better performance if you could index "SomeProperty" and use a dexie operator to check it instead of using filter, see example snippet below:
// Indexing a property:
this.indexdb = new Dexie('yourDbName');
this.indexdb.version(x).stores({
tableName: "id,SomeProperty" // index SomeProperty
});
// Query utilizing the index instead of using filter():
await this.indexdb.TableName.where('SomeProperty').equals(Property).toArray();
Utilizing indexes is the whole idea with indexedDB especially as the data grows. There are other operators than equals(). See Dexie's WhereClause docs
If your queries are too complex to be used with dexie operators, you'd probably be much better of using this.indexdb.TableName.toArray().then(result => result.filter(yourCriteriaFn))
rather than this.indexdb.TableName.filter(yourCriteriaFn).toArray()
because Dexie will then be able to skip cursor iteration for every row and instead use the getAll() method that produces a single event instead of one per row.
See also an issue in dexie about the slowness that can occur specifically in Angular and IndexedDB
Upvotes: 4