Datenkonsistenz in Datastore-Abfragen
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Datenkonsistenzebenen
Die Ergebnisse von Datenspeicherabfragen können mit einer von zwei Konsistenzebenen bereitgestellt werden:
- Strikt konsistente Abfragen (Strong Consistency) garantieren die aktuellsten Ergebnisse, ihre Verarbeitung kann jedoch länger dauern.
- Abfragen mit Eventual Consistency werden im Allgemeinen schneller ausgeführt, können jedoch gelegentlich veraltete Ergebnisse zurückgeben.
Bei einer Abfrage mit Eventual Consistency wird auf die Indexe, von denen die Ergebnisse abgerufen werden, ebenfalls mit Eventual Consistency zugegriffen. Folglich geben solche Abfragen manchmal Entitäten zurück, die nicht mehr den ursprünglichen Abfragekriterien entsprechen, während Abfragen mit Strong Consistency immer konsistent sind.
Datenkonsistenz in Datastore-Abfragen
Die Ergebnisse von Abfragen werden je nach Art der Abfrage mit unterschiedlichen Konsistenzgarantieebenen zurückgegeben:
- Ancestor-Abfragen sind Abfragen innerhalb einer Entitätengruppe und standardmäßig strikt konsistent. Sie können jedoch durch Anpassung der Leserichtlinien für Datastore zu Eventual Consistency-Abfragen werden (siehe unten).
- Nicht-Ancestor-Abfragen sind immer Eventual-Consistency-Abfragen.
Der Abruf einer Entität nach Schlüssel, auch „Suche nach Schlüssel“ genannt, ist stark konsistent.
Leserichtlinie für Datastore einrichten
Zur Verbesserung der Leistung können Sie die Leserichtlinien für Datastore so festlegen, dass alle Lesevorgänge und Abfragen mit Eventual Consistency ausgeführt werden. (Mit der API können Sie auch explizit eine Richtlinie für strikte Konsistenz festlegen. Diese Einstellung hat jedoch keine praktischen Auswirkungen, weil Nicht-Ancestor-Abfragen ungeachtet der Richtlinie immer mit Eventual Consistency ausgeführt werden.)
Sie können auch ein
Zeitlimit für Aufrufe in Datastore festlegen. Dies ist die maximale Zeit in Sekunden, die die Anwendung wartet, bis Datastore ein Ergebnis zurückgibt, bevor sie mit einem Fehler abbricht. Das Zeitlimit beträgt normalerweise 60 Sekunden. Höhere Werte sind derzeit nicht möglich. Sie können das Limit jedoch verkürzen und so dafür sorgen, dass ein bestimmter Vorgang schnell fehlschlägt (z. B. damit der Nutzer eine schnellere Antwort erhält).
Zum Festlegen der Datastore-Leserichtlinie und des Zeitlimits für Aufrufe in Python übergeben Sie sie als Argumente an die Methoden
run()
,
get()
,
fetch()
und
count()
der Klasse
Query
oder
GqlQuery
. Beispiel:
for result in Employee.all().run(limit=5,
read_policy=db.EVENTUAL_CONSISTENCY,
deadline=5):
# Body of iterative loop
Weitere Informationen
Sofern nicht anders angegeben, sind die Inhalte dieser Seite unter der Creative Commons Attribution 4.0 License und Codebeispiele unter der Apache 2.0 License lizenziert. Weitere Informationen finden Sie in den Websiterichtlinien von Google Developers. Java ist eine eingetragene Marke von Oracle und/oder seinen Partnern.
Zuletzt aktualisiert: 2025-09-04 (UTC).
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-09-04 (UTC)."],[[["\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."]]