简体   繁体   English

Firestore 数据库读取/写入非常慢

[英]Firestore database reads/writes incredibly slow

OS: Mac OS Catalina v 10.15.1操作系统:Mac OS Catalina v 10.15.1

Python version: Python 3.7.1 Python 版本:Python 3.7.1

Firestore pip package version: google-cloud-firestore 1.6.1 Firestore pip 包版本:google-cloud-firestore 1.6.1

I'm testing simple database reads and writes on a very simple Firestore database.我正在一个非常简单的 Firestore 数据库上测试简单的数据库读写。 I'm looking at a single collection with a single document containing 1 data item (a string);我正在查看包含 1 个数据项(字符串)的单个文档的单个集合; however, when I try to write (as shown below), the timer I have (also in the below code) shows that the first write takes over 30 seconds .但是,当我尝试写入时(如下所示),我拥有的计时器(也在下面的代码中)显示第一次写入需要超过 30 秒 This same behavior is present for reads.对于读取,存在相同的行为。

However, this is only present for the first write/read in this python script.但是,这仅适用于此 python 脚本中的第一次写入/读取。 All subsequent writes/reads are timed at roughly 100ms (still slow for such a tiny database).所有后续写入/读取的时间大约为 100 毫秒(对于如此小的数据库来说仍然很慢)。 I have not been able to find this issue replicated on the internet, and there is no mention of behavior like this in the Firestore Python SDK documentation.我没能在互联网上找到这个问题的复制,Firestore Python SDK 文档中也没有提到这样的行为。 I am running the below code locally on my machine.我在我的机器上本地运行以下代码。

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
import datetime

cred = credentials.Certificate("./path/to/adminsdk.json")
firebase_admin.initialize_app(cred)
db = firestore.client()

start = datetime.datetime.now()

#the below write takes >30 seconds to execute
doc_ref = db.collection(u'test_collection').document(u'test_document')
doc_ref.set({u'test_field':'data'})

time_after_first = datetime.datetime.now()
print(time_after_first - start)

#The below read takes 80 ms to execute
doc_data = doc_ref.get()
print(doc_data.get('test_field'))

print(datetime.datetime.now()-time_after_first)

print("done")

I found a suggestion on another StackOverflow post suggesting to use on_snapshot instead of get ;我在另一个 StackOverflow 帖子上发现了一个建议,建议使用on_snapshot而不是get however, the problem persists (the DocumentSnapshot is only received after roughly 30 seconds).但是,问题仍然存在(大约 30 秒后才收到 DocumentSnapshot)。 Additionally, the problem is not unique to get , but for set , update , etc. as well.此外,问题不仅限于get ,也适用于setupdate等。

Any tips would be appreciated!任何提示将不胜感激!

You should think of your connection performance.您应该考虑您的连接性能。 I have implemented this code on my side and here are effects.我已经在我这边实现了这个代码,这里是效果。 It never reached 0.5s.它从未达到0.5s。 Seems Firestore performance is very good.看来Firestore的性能非常好。 I hope this will help you solve it...我希望这会帮助你解决它...

在此处输入图片说明

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

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