使用Python从Google firestore读取数据时,每日读取次数增长过快

2024-06-16 14:59:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个从Google firestore数据库读取数据的程序。 数据库包含不同用户的数据,程序的每个实例都应该只读取指定用户的数据

数据的组织方式如下:

UsersInfo (Collection)
|________User01 (document)
|________User02 (document)
...
|________UserN (document)

其中每个用户文档都包含一个标识ID

程序第一次运行时,会初始化数据库,并通过以下方式查找包含用户信息的正确文档:

cred = credentials.Certificate(fcredentials_file.json)
firebase_admin.initialize_app(cred)
db = firestore.client()


docs = db.collection(u'UsersInfo').stream()
user_found = False
current_user_document = ''
## find the right document, based on user_ID
try:
    for doc in docs:
        if doc.to_dict()['Userid'] == user_ID:
            current_user_document = doc.id
            user_found = True
            print(f"User found in document {current_user_document}")
            break
except:
    print("Impossible to find user in firestore!!!")

此时,已找到所需用户的正确文档。 此信息会传递给系统中的其他进程,这些进程会定期检查此文档以检索某些信息,例如:

doc_ref = db.collection(u'UserInfo').document(UserXX)
return doc_ref.get().to_dict()['some_field']

我原以为:

  • 在初始化过程中,程序检查集合中的所有UserXX文档(大约有50个)——>;50读
  • 每次其他进程检查已识别的用户文档时,它都被视为另一次读取

然而,报告的读取量正在飙升…我今天运行了几次系统,每次它执行初始化,其他组件检查用户文档4或5次…但现在使用情况报告11K读取! 是我做错了什么,还是我误解了什么才算是阅读


Tags: 数据用户文档程序信息id数据库db
1条回答
网友
1楼 · 发布于 2024-06-16 14:59:24

仅这一行就需要对集合中的每个文档进行一次读取:

docs = db.collection(u'UsersInfo').stream()

接下来做什么无关紧要——所有文档现在都已被读取并可在内存中使用

如果只在集合中查找其Userid字段包含特定值的文档,则应在该字段上query the collection using a filter

docs = db.collection(u'UsersInfo').where(u'Userid', u'==', user_ID).stream()

相关问题 更多 >