Datastore 查询中的数据一致性
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
数据一致性级别
Datastore 查询可以按以下任何一种一致性级别交付结果:
- 强一致性查询可以保证获得最新的结果,但可能需要更长时间才能完成。
- 具备最终一致性的查询通常运行速度更快,但有时会返回过时的结果。
在最终一致性查询中,也会按最终一致性访问用于收集结果的索引。因此,此类查询有时可能返回不再符合最初查询条件的实体,而高度一致性查询在事务上始终一致。
Datastore 查询数据一致性
查询返回的结果具有不同级别的一致性保证,具体取决于查询的性质:
- 祖先查询(实体组内的查询)在默认情况下高度一致,但可以通过设置 Datastore 读取政策将其改为最终一致(见下文)。
- 非祖先查询总是最终一致的。
按键提取实体(也称为“按键查找”)具有高度一致性。
设置 Datastore 读取政策
为了提高性能,您可以设置 Datastore 读取政策以使所有读取和查询最终一致。(API 还可让您明确设置强一致性政策,但此设置没有实际影响,因为无论政策如何,非祖先查询总是具有最终一致性)。
您还可以设置 Datastore 最后调用期限
,这是应用在取消调用并返回错误之前等待 Datastore 返回结果的最长时间(以秒为单位)。默认期限为 60 秒;目前无法设置为更大的值,但您可以将其向下调整,以确保特定操作快速失败(例如,更快地向用户返回响应)。
如需使用 Python 设置 Datastore 读取政策和最后调用期限,请将其作为参数传递给
Query
或
GqlQuery
类的
run()
、
get()
、
fetch()
和
count()
方法。例如:
for result in Employee.all().run(limit=5,
read_policy=db.EVENTUAL_CONSISTENCY,
deadline=5):
# Body of iterative loop
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-04-01。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-04-01。"],[[["Datastore queries operate at two consistency levels: strongly consistent, which guarantees the freshest data but may take longer, and eventually consistent, which is generally faster but might return stale data."],["Ancestor queries, which occur within an entity group, are strongly consistent by default but can be made eventually consistent by adjusting the Datastore read policy, whereas non-ancestor queries are always eventually consistent."],["Fetching an entity by key, also known as \"lookup by key\", provides strong consistency in retrieving data."],["The Datastore read policy can be set to ensure all reads and queries are eventually consistent, optimizing performance, and while a strong consistency policy can be set, it has no effect on non-ancestor queries, as they remain eventually consistent."],["The Datastore call deadline, the maximum time an application waits for a result, defaults to 60 seconds but can be reduced to ensure faster operation failures, thus allowing the possibility of faster user responses."]]],[]]