Python 2.7 已終止支援,並將於 2026 年 1 月 31 日
淘汰。淘汰後,您將無法部署 Python 2.7 應用程式,即使貴機構先前曾使用機構政策重新啟用舊版執行階段的部署作業,也無法部署。現有的 Python 2.7 應用程式在
淘汰日期過後,仍會繼續執行並接收流量。建議您
改用系統支援的最新 Python 版本。
Datastore 查詢中的資料一致性
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
資料一致性等級
Datastore 查詢能夠提供的結果分為兩種一致性等級:
- 「同步一致性」查詢可保證獲得最新的結果,但可能需要較長的時間才能完成。
- 最終一致性查詢的執行速度通常較快,但偶爾可能會傳回過時的結果。
在最終一致性查詢中,用於收集結果的索引也是透過最終一致性的方式來存取。因此,這類查詢有時候會傳回不再符合原始查詢條件的實體,而具有同步一致性的查詢則一律具有交易一致性。
Datastore 查詢資料一致性
根據查詢的性質,查詢會傳回一致性保證等級不同的結果:
- 根據預設,祖系查詢 (在實體群組內) 會維持同步一致性,但可以藉由設定 Datastore 讀取政策 (請見下文) 改為維持最終一致性。
- 非祖系查詢始終會維持最終一致性。
按金鑰擷取實體 (也稱為「按金鑰查詢」) 會維持同步一致性。
設定 Datastore 讀取政策
為改善效能,您可以設定 Datastore 讀取政策,讓所有讀取和查詢都具有最終一致性。(您也可以透過 API 明確設定同步一致性政策,但是這樣的設定並無實質效果,因為無論政策為何,非祖系查詢一律會採取最終一致性。)
您也可以設定 Datastore 的「呼叫期限」
,即為應用程式等待 Datastore 傳回結果的時間上限 (以秒為單位),超過此期限就會因為錯誤而取消。預設值為 60 秒,目前無法設為高於此上限,但可往下調整以確保特定作業快速失敗 (例如更快速傳回回應給使用者)。
如要在 Python 中設定資料儲存庫讀取政策和呼叫期限,請將這些項目做為引數傳遞至
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
後續步驟
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-09-04 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-04 (世界標準時間)。"],[[["\u003cp\u003eDatastore 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.\u003c/p\u003e\n"],["\u003cp\u003eAncestor 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.\u003c/p\u003e\n"],["\u003cp\u003eFetching an entity by key, also known as "lookup by key", provides strong consistency in retrieving data.\u003c/p\u003e\n"],["\u003cp\u003eThe 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.\u003c/p\u003e\n"],["\u003cp\u003eThe 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.\u003c/p\u003e\n"]]],[],null,["# Data Consistency in Datastore Queries\n\nData consistency levels\n-----------------------\n\nDatastore queries can deliver their results at either of two consistency\nlevels:\n\n- [*Strongly consistent*](https://en.wikipedia.org/wiki/Strong_consistency) queries guarantee the freshest results, but may take longer to complete.\n- [*Eventually consistent*](https://en.wikipedia.org/wiki/Eventual_consistency) queries generally run faster, but may occasionally return stale results.\n\nIn an eventually consistent query, the indexes used to gather the results are also accessed with eventual consistency. Consequently, such queries may sometimes return entities that no longer match the original query criteria, while strongly consistent queries are always transactionally consistent.\n\nDatastore query data consistency\n--------------------------------\n\nQueries return their results with different levels of consistency guarantee, depending on the nature of the query:\n\n- [Ancestor queries](/appengine/docs/legacy/standard/python/datastore/queries#ancestor_queries) (those within an [entity group](/appengine/docs/legacy/standard/python/datastore/entities#Ancestor_paths)) are strongly consistent by default, but can instead be made eventually consistent by setting the Datastore read policy (see below).\n- Non-ancestor queries are always eventually consistent.\n\nFetching an entity by key, which is also called \"lookup by key\", is strongly\nconsistent.\n\n\u003cbr /\u003e\n\nSetting the Datastore read policy\n---------------------------------\n\nTo improve performance, you can set the Datastore *read policy* so that all reads and queries are eventually consistent. (The API also allows you to explicitly set a strong consistency policy, but this setting will have no practical effect, since non-ancestor queries are always eventually consistent regardless of policy.)\nYou can also set the Datastore *call deadline* , which is the maximum time, in seconds, that the application will wait for Datastore to return a result before aborting with an error. The default deadline is 60 seconds; it is not currently possible to set it higher, but you can adjust it downward to ensure that a particular operation fails quickly (for instance, to return a faster response to the user).\n\n\u003cbr /\u003e\n\nTo set the Datastore read policy and call deadline in Python, you pass them as arguments to the [`run()`](/appengine/docs/legacy/standard/python/datastore/queryclass#Query_run), [`get()`](/appengine/docs/legacy/standard/python/datastore/queryclass#Query_get), [`fetch()`](/appengine/docs/legacy/standard/python/datastore/queryclass#Query_fetch), and [`count()`](/appengine/docs/legacy/standard/python/datastore/queryclass#Query_count) methods of class [`Query`](/appengine/docs/legacy/standard/python/datastore/queryclass) or [`GqlQuery`](/appengine/docs/legacy/standard/python/datastore/gqlqueryclass). For example:\n\n\u003cbr /\u003e\n\n for result in Employee.all().run(limit=5,\n read_policy=db.EVENTUAL_CONSISTENCY,\n deadline=5):\n # Body of iterative loop\n\nWhat's next?\n------------\n\n- [Learn how to specify what a query returns and further control query results](/appengine/docs/legacy/standard/python/datastore/retrieving-query-results).\n- Learn the [common restrictions](/appengine/docs/legacy/standard/python/datastore/query-restrictions) for queries on Datastore.\n- Learn about [query cursors](/appengine/docs/legacy/standard/python/datastore/query-cursors), which allow an application to retrieve a query's results in convenient batches.\n- Learn the [basic syntax and structure of queries](/appengine/docs/legacy/standard/python/datastore/queries) for Datastore."]]