简体   繁体   English

Firestore 性能

[英]Firestore performance

Is there a way to set a collection to have integer ids in all its documents?有没有办法将集合设置为在其所有文档中都有 integer 个 ID? I am looking for something like the autoincrement id of SQL tables.我正在寻找类似 SQL 表的自动增量 ID 之类的东西。 If not, which of these options have better performance?如果不是,这些选项中哪个有更好的性能?

for (let i = 0; i < 1000; i++) {
    // option 1
    firestore.collection("documents").doc(`${i}`).set(documents[i])
    // option 2
    firestore.collection("documents").add({ intId: i })
}

Firestore does not provide any sort of autoincrement IDs. Firestore 不提供任何类型的自动增量 ID。 Those do not scale in the way that Firestore requires.这些不会按照 Firestore 要求的方式扩展。

Both options you show have the same performance characteristics.您显示的两个选项具有相同的性能特征。 The limiting factor here is described in thedocumentation about hotspotting behavior on document writes:此处的限制因素在有关文档写入的热点行为的文档中进行了描述:

Avoid high read or write rates to lexicographically close documents, or your application will experience contention errors.避免按字典顺序关闭文档的高读取或写入速率,否则您的应用程序将遇到争用错误。

This is exactly what your code is doing.这正是您的代码正在做的事情。 It's suggested that you use random IDs rather than sequential IDs, since sequential IDs don't scale well for Firestore.建议您使用随机 ID 而不是顺序 ID,因为顺序 ID 不能很好地扩展到 Firestore。

You should read more of the linked documentation to learn more specifically about hotspotting.您应该阅读更多链接文档以更具体地了解热点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM