[英]Firestore Query performance issue on Firebase Cloud Functions
I am facing timeout issues on a firebase https function so I decided to optimize each line of code and realized that a single query is taking about 10 seconds to complete.我在 firebase https function 上遇到超时问题,所以我决定优化每一行代码并意识到单个查询大约需要 10 秒才能完成。
let querySnapshot = await admin.firestore()
.collection("enrollment")
.get()
The enrollment collection has about 23k documents, totaling approximately 6MB.注册集合有大约 23k 个文档,总计大约 6MB。
To my understanding, since the https function is running on a cloud function stateless server, it should not suffer from the query result size.据我了解,由于 https function 运行在云 function 无状态服务器上,因此它不应该受到查询结果大小的影响。 Both Firestore and Cloud Functions are running on the same region (us-central). Firestore 和 Cloud Functions 都在同一区域 (us-central) 上运行。 Yet 10 seconds is indeed a high interval of time for executing such a simple query that results in a small snapshot size.然而,10 秒确实是执行这种导致小快照大小的简单查询的高时间间隔。
An interesting fact is that later in the code I update those 23k documents back with a new field using Bulk Writter and it takes less than 3 seconds to run bulkWriter.commit().一个有趣的事实是,在代码的后面,我使用 Bulk Writer 用一个新字段更新了那些 23k 文档,运行 bulkWriter.commit() 只需不到 3 秒。
Another fact is that the https function is not returning any of the query results to the client, so there shouldn't be any "downloading" time affecting the function performance.另一个事实是 https function 没有向客户端返回任何查询结果,因此不应该有任何影响 function 性能的“下载”时间。
Why on earth does it take 3x longer to read values from a collection than writing to it?为什么从集合中读取值比写入值要长 3 倍? I always thought Firestore architecture was meant for apps with high reading rates rather than writing.我一直认为 Firestore 架构适用于具有高读取率的应用程序,而不是写入。
Is there anything you would propose to optimize this?您有什么建议可以对此进行优化吗?
When we perform the get(), a query is created to all document snapshots and the results are returned .当我们执行 get() 时,会创建一个对所有文档快照的查询并返回结果。 These results are fetched sequentially within a single execution, ie the list is returned and parsed sequentially until all documents have been listed.这些结果在单次执行中按顺序获取,即按顺序返回和解析列表,直到列出所有文档。
While the data may be small, are there any subcollections?虽然数据可能很小,但是否有任何子集合? This may add some additional latency as the API fetches and parses subcollections.这可能会增加一些额外的延迟,因为 API 获取并解析子集合。
Updating the fields with a bulk writer update is over 3x the speed because the bulkwriter operation is performed in parallel and is queued based upon Promises.使用批量写入器更新字段的速度是原来的 3 倍以上,因为批量写入器操作是并行执行的,并且根据 Promises 进行排队。 This allows many more operations per second.这允许每秒进行更多操作。
The best way to optimize listing all documents is summarised in this link , and Google's recommendation follows the same guideline being to use an index for faster queries and to use multiple readers that fetch the documents in parallel. 此链接总结了优化列出所有文档的最佳方法,Google 的建议遵循相同的准则,即使用索引来加快查询速度并使用多个阅读器并行获取文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.