A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cloud.google.com/appengine/docs/legacy/standard/python/datastore/projectionqueries below:

Projection Queries | App Engine standard environment for Python 2

Projection Queries

Stay organized with collections Save and categorize content based on your preferences.

Note: Developers building new applications are strongly encouraged to use the NDB Client Library, which has several benefits compared to this client library, such as automatic entity caching via the Memcache API. If you are currently using the older DB Client Library, read the DB to NDB Migration Guide

Most Datastore queries return whole entities as their results, but often an application is actually interested in only a few of the entity's properties. Projection queries allow you to query Datastore for just those specific properties of an entity that you actually need, at lower latency and cost than retrieving the entire entity.

Projection queries are similar to SQL queries of the form:

SELECT name, email, phone FROM CUSTOMER

You can use all of the filtering and sorting features available for standard entity queries, subject to the limitations described below. The query returns abridged results with only the specified properties (name, email, and phone in the example) populated with values; all other properties have no data.

Using projection queries in Python 2

You specify a projection this way: Projection queries are supported by both Query and GqlQuery objects. Both classes require this import:

from google.appengine.ext import db

You specify a projection this way:

proj = db.Query(entity_name, projection=('property_1', 'property_2','property_n'))

proj = db.GqlQuery("SELECT property_1, property_2, property_n FROM entity_name")

You handle the results of these queries just as you would for a standard entity query: for example, by iterating over the results.

The following example queries for the title, read_path, and date_written properties of all EventLog entries, sorted in ascending order by date_written, and writes each property's value to the application log:

for proj in db.GqlQuery("SELECT title, read_path, date_written" +
                        "FROM EventLog" +
                        "ORDER BY date_written ASC"):
  logging.info(proj.title)
  logging.info(proj.read_path)
  logging.info(proj.date_written)
Grouping(experimental)

Projection queries can use the distinct keyword to ensure that only completely unique results will be returned in a result set. This will only return the first result for entities which have the same values for the properties that are being projected.

query = db.Query(projection=['A', 'B'], distinct=True).filter('B >', 1).order('-B, A')
Limitations on projections

Projection queries are subject to the following limitations:

Projections and multiple-valued properties

Projecting a property with multiple values will not populate all values for that property. Instead, a separate entity will be returned for each unique combination of projected values matching the query. For example, suppose you have an entity of kind Foo with two multiple-valued properties, A and B:

Then the projection query

SELECT A, B FROM Foo WHERE A < 3

will return four entities with the following combinations of values:

A = 1, B = 'x'
A = 1, B = 'y'
A = 2, B = 'x'
A = 2, B = 'y'

Note that if an entity has a multiple-valued property with no values, no entries will be included in the index, and no results for that entity will be returned from a projection query including that property.

Warning: Including more than one multiple-valued property in a projection will result in an exploding index. Indexes for projections

Projection queries require all properties specified in the projection to be included in a Datastore index. The App Engine development server automatically generates the needed indexes for you in the index configuration file, index.yaml, which is uploaded with your application.

One way to minimize the number of indexes required is to project the same properties consistently, even when not all of them are always needed. For example, these queries require two separate indexes:

SELECT A, B FROM Kind
SELECT A, B, C FROM Kind

However, if you always project properties A, B, and C, even when C is not required, only one index will be needed.

Converting an existing query into a projection query may require building a new index if the properties in the projection are not already included in another part of the query. For example, suppose you had an existing query like

SELECT * FROM Kind WHERE A > 1 ORDER BY A, B

which requires the index

Index(Kind, A, B)

Converting this to either of the projection queries

SELECT C FROM Kind WHERE A > 1 ORDER BY A, B
SELECT A, B, C FROM Kind WHERE A > 1 ORDER BY A, B

introduces a new property (C) and thus will require building a new index Index(Kind, A, B, C). Note that the projection query

SELECT A, B FROM Kind WHERE A > 1 ORDER BY A, B

would not change the required index, since the projected properties A and B were already included in the existing query.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-08-07 UTC.

[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-07 UTC."],[[["Projection queries allow for retrieving only specific properties of an entity from Datastore, reducing latency and cost compared to fetching entire entities."],["These queries are specified using `db.Query` or `db.GqlQuery` objects, defining which properties to retrieve from a given entity."],["Projection queries support filtering and sorting features similar to standard entity queries, but are restricted to only indexed properties."],["Results from projection queries cannot be saved back to Datastore, as they are only partially populated."],["Using multiple-valued properties in projections results in a separate entity for each unique combination of projected values, and all projected properties must be included in a Datastore index."]]],[]]


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4