Dexie db 随着时间的推移变慢

mah*_*ani 2 chromium indexeddb dexie electron angular

我正在将 Dexie DB 版本 2.0.4 与 Angular 8 和 Electron 一起使用。我意识到,在数据库中输入的数据越多,对其进行的查询越多,数据库调用就会变得越慢。

我是一个 Dexie 菜鸟,不确定在发出某些请求后是否有办法关闭连接,或者是否有东西卡在内存中。

任何人都知道如何调试或确认问题是什么?

这是我向数据库提出的请求之一的示例:

await this.indexdb.TableName.filter(SomeProperty => SomeProperty == Property).toArray();
await this.indexdb.TableName.put({'SomePropertyName':'SomePropertyValue'})
Run Code Online (Sandbox Code Playgroud)

多谢

Dav*_*der 5

过滤方法不使用任何索引,而是使用游标遍历表中的所有行来测试您的条件。这可能会变得很慢,尤其是在使用 Zone.js(它是 Angular 的一部分)时,它会拦截所有 IndexedDB 事件,并对整个应用程序针对根据过滤条件进行测试的每一行进行更改检测。

如果您可以索引“SomeProperty”并使用 dexie 运算符来检查它而不是使用过滤器,您将获得更好的性能,请参阅下面的示例片段:

// 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();
Run Code Online (Sandbox Code Playgroud)

利用索引是indexedDB 的整体理念,尤其是随着数据的增长。除了 equals() 之外还有其他运算符。请参阅Dexie 的WhereClause 文档

如果您的查询太复杂而无法与 dexie 运算符一起使用,那么您可能会更好地使用this.indexdb.TableName.toArray().then(result => result.filter(yourCriteriaFn))而不是this.indexdb.TableName.filter(yourCriteriaFn).toArray()因为 Dexie 将能够跳过每一行的游标迭代,而是使用生成单个事件的 getAll() 方法而不是每行一个。

另请参阅dexie 中有关 Angular 和 IndexedDB 中特别可能发生的缓慢问题