我想尝试将Firestore用作我的Android应用程序的后端,并将用于存储UPC(条形码)和我希望应用程序的所有用户均可使用的其他属性。目前,我执行以下步骤(高级步骤和下面的示例Python代码)。
以下是有问题的代码-
upc = sys.argv[1]
nutrition = json.loads(sys.argv[2].replace("'", '"'))
start_ms = int(round(time.time() * 1000))
print(f'[INFO] Checking if document exists for UPC: {upc}')
upc_doc = db.collection(u'upcs').document(upc)
doc = upc_doc.get()
print(f'[INFO] Existing document found - check nutrition attributes now.')
# If the UPC exists, let's update confirmedScans
versions = db.collection(u'upcs').document(upc).collection(u'versions')
nutrition_query = (versions.where(u'property0', u'==', nutrition['property0'])
.where(u'property1', u'==', nutrition['property1'])
.where(u'property2', u'==', nutrition['property2'])
.where(u'property3', u'==', nutrition['property3'])
.where(u'property4', u'==', nutrition['property4'])
.where(u'property5', u'==', nutrition['property5'])
.where(u'property6', u'==', nutrition['property6']))
nutrition_results = nutrition_query.get()
numberOfResults = 0
for nutrition_result in nutrition_results:
numberOfResults += 1
full_result = nutrition_result
if numberOfResults == 0:
versions.document()
versions.set(nutrition)
else:
print('[INFO] Existing document found for combination of UPC & nutrition - increment scan number.')
full_result_dict = full_result.to_dict()
confirmedScans = full_result_dict['confirmedScans']
confirmedScans = confirmedScans + 1
print(f'[INFO] Setting document {full_result.id} to confirmedScans: {confirmedScans}.')
doc_to_update = versions.document(full_result.id)
update_result = doc_to_update.update({u'confirmedScans': confirmedScans})
print(update_result)
end_ms = int(round(time.time() * 1000))
total_ms = end_ms - start_ms
print(f'[INFO] Duration (ms): {total_ms}')
目前,这段代码的Python概念验证部分大约需要800毫秒-1000毫秒,这并不可怕,但是我想知道是否有什么我可以做的事情来加快我的查询或连接的速度,例如在两者中创建索引集合或文件本身。
如果这是最快的,它将根据上面的代码运行,是否有更好的技术可以实现?也许是某种类型的RDBMS或列式数据存储?