【问题标题】:Reading data from Google firestore with Python, daily reads count increases too fast使用 Python 从 Google Firestore 读取数据,每日读取次数增加过快
【发布时间】:2020-12-18 22:13:13
【问题描述】:

我有一个程序从 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 读取! 是我做错了什么,还是我误解了什么才算是阅读?

【问题讨论】:

  • 查看查询。

标签: python google-cloud-firestore


【解决方案1】:

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

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-08
    • 2019-07-29
    • 1970-01-01
    • 2018-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多