Firestore Data Structure Design: Difference in Query performance using .where('IdField','==', id) vs .doc(id)

huangapple go评论68阅读模式
英文:

Firestore Data Structure Design: Difference in Query performance using .where('IdField','==', id) vs .doc(id)

问题

Firestore查询中使用.where()和查找特定doc.id之间有什么区别?

假设我可以以两种方式组织我的集合:

1)具有doc.id = myspecificIDstringcollection1
我可以像这样查询:db.collection('collection1').doc('myspecificIDstring')

2)具有名为"theID":"myspecificIDstring"的字段的collection2
我可以像这样查询:db.collection('collection2').where('theID','==','myspecificIDstring')

在读取/性能方面是否有任何区别?我正在尝试决定如何构建我的后端。

我已尝试了这两种方式,它们似乎是相同的。

英文:

What's the difference between querying firestore using .where() and looking up a specific doc.id?

say I can choose to structure my collection in 2 ways:

  1. collection1 that has doc.id = myspecificIDstring
    I can query like db.collection('collection1').doc('myspecificIDstring')

  2. collection2 that has a field called "theID":"myspecificIDstring"
    I can query like db.collection('collection2').where('theID','==','myspecificIDstring')

are there any differences in reads / performance? I'm trying to decide how to structure my backend

I've tried both ways and they are seemingly the same.

答案1

得分: 0

  • .doc('myspecificIDstring'):这个方法提供了对文档的直接引用,从而进行一次读取操作以检索指定的文档。它非常适用于快速获取已知文档。

  • .where('theID', '==', 'myspecificIDstring'):这个方法执行一个查询并在整个集合上产生读取操作。Firestore 索引用于高效地缩小结果范围,以匹配指定条件的文档。如果在 'theID' 字段上有索引,性能应该是合理的。然而,如果在没有适当索引的情况下查询大型集合,可能会导致延迟增加和更高的成本。

所以,.doc 更高效。

英文:
  • .doc('myspecificIDstring'): This method provides a direct reference to the document, resulting in a single read operation to retrieve the specified document. It is efficient for fetching a known document quickly.

  • .where('theID', '==', 'myspecificIDstring'): This method performs a query and incurs a read operation on the entire collection. Firestore indexes are used to efficiently narrow down the results to documents matching the specified condition. If you have an index on the 'theID' field, the performance should be reasonable. However, querying a large collection without an appropriate index may lead to increased latency and higher costs.

So, .doc is more efficient.

答案2

得分: 0

如果查询仅匹配单个文档,加载单个文档的两种方法之间没有显著的性能差异。

这是因为Firestore在两种情况下都将使用相同的方法来加载文档:在相关索引中查找文档ID或查询值,然后加载与之匹配的文档。

我通常会选择第一种方法,但这是为了代码清晰度的原因,而不是性能上可能的差异。

英文:

If the query also matches only a single document, there is no significant performance difference between the two methods of loading a single document.

This is because Firestore will in both cases use the same approach to load the document: looking up either the document ID or the query value in the relevant index, and then loading the document that matches it.

I'd usually pick the first approach, but that is for reasons of code clarity, not for a possible difference in performance.

huangapple
  • 本文由 发表于 2023年5月29日 16:30:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76355771.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定