[[["容易理解","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\u003eProjection queries allow for retrieving only specific properties of an entity from Datastore, reducing latency and cost compared to fetching entire entities.\u003c/p\u003e\n"],["\u003cp\u003eThese queries are specified using \u003ccode\u003edb.Query\u003c/code\u003e or \u003ccode\u003edb.GqlQuery\u003c/code\u003e objects, defining which properties to retrieve from a given entity.\u003c/p\u003e\n"],["\u003cp\u003eProjection queries support filtering and sorting features similar to standard entity queries, but are restricted to only indexed properties.\u003c/p\u003e\n"],["\u003cp\u003eResults from projection queries cannot be saved back to Datastore, as they are only partially populated.\u003c/p\u003e\n"],["\u003cp\u003eUsing 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.\u003c/p\u003e\n"]]],[],null,["# Projection Queries\n\n**Note:**\nDevelopers building new applications are **strongly encouraged** to use the\n[NDB Client Library](/appengine/docs/legacy/standard/python/ndb), which has several benefits\ncompared to this client library, such as automatic entity caching via the Memcache\nAPI. If you are currently using the older DB Client Library, read the\n[DB to NDB Migration Guide](/appengine/docs/legacy/standard/python/ndb/db_to_ndb)\n\nMost Datastore [queries](/appengine/docs/legacy/standard/python/datastore/queries) return whole [entities](/appengine/docs/legacy/standard/python/datastore/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.\n\nProjection queries are similar to SQL queries of the form: \n\n SELECT name, email, phone FROM CUSTOMER\n\nYou can use all of the filtering and sorting features available for standard entity queries, subject to the [limitations](#Limitations_on_projections) 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.\n\nUsing projection queries in Python 2\n------------------------------------\n\nYou specify a projection this way:\nProjection queries are supported by both [Query](/appengine/docs/legacy/standard/python/datastore/queryclass) and [GqlQuery](/appengine/docs/legacy/standard/python/datastore/gqlqueryclass) objects. Both classes require this import: \n\n from google.appengine.ext import db\n\nYou specify a projection this way: \n\n proj = db.Query(entity_name, projection=('property_1', 'property_2','property_n'))\n\n proj = db.GqlQuery(\"SELECT property_1, property_2, property_n FROM entity_name\")\n\nYou handle the results of these queries just as you would for a standard entity query: for example, by iterating over the results.\n\nThe 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: \n\n for proj in db.GqlQuery(\"SELECT title, read_path, date_written\" +\n \"FROM EventLog\" +\n \"ORDER BY date_written ASC\"):\n logging.info(proj.title)\n logging.info(proj.read_path)\n logging.info(proj.date_written)\n\nGrouping^(experimental)^\n------------------------\n\nProjection 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. \n\n query = db.Query(projection=['A', 'B'], distinct=True).filter('B \u003e', 1).order('-B, A')\n\nLimitations on projections\n--------------------------\n\nProjection queries are subject to the following limitations:\n\n- **Only indexed properties can be projected.**\n\n Projection is not supported for properties that are not indexed, whether explicitly or implicitly. Long text strings ([`Text`](/appengine/docs/legacy/standard/python/ndb/entity-property-reference#types)) and long byte strings ([`Blob`](/appengine/docs/legacy/standard/python/ndb/entity-property-reference#types)) are not indexed.\n- **The same property cannot be projected more than once.**\n\n- **Properties referenced in an equality (`=`) or membership (`IN`) filter cannot be projected.**\n\n For example, \n\n SELECT A FROM kind WHERE B = 1\n\n is valid (projected property not used in the equality filter), as is \n\n SELECT A FROM kind WHERE A \u003e 1\n\n (not an equality filter), but \n\n SELECT A FROM kind WHERE A = 1\n\n (projected property used in equality filter) is not.\n- **Results returned by a projection query cannot be saved back to Datastore.**\n\n Because the query returns results that are only partially populated, you cannot write them back to Datastore.\n\nProjections and multiple-valued properties\n------------------------------------------\n\nProjecting 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`: \n\n entity = Foo(A=[1, 1, 2, 3], B=['x', 'y', 'x'])\n\nThen the projection query \n\n SELECT A, B FROM Foo WHERE A \u003c 3\n\nwill return four entities with the following combinations of values:\n\n`A` = `1`, `B` = `'x'` \n\n`A` = `1`, `B` = `'y'` \n\n`A` = `2`, `B` = `'x'` \n\n`A` = `2`, `B` = `'y'`\n\nNote that if an entity has a multiple-valued property with no values, no entries\nwill be included in the index, and no results for that entity will be returned\nfrom a projection query including that property.\n| **Warning:** Including more than one multiple-valued property in a projection will result in an [exploding index](/appengine/docs/legacy/standard/python/datastore/indexes#index-limits).\n\nIndexes for projections\n-----------------------\n\nProjection queries require all properties specified in the projection to be included in a Datastore [index](/appengine/docs/legacy/standard/python/datastore/indexes). 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.\n\nOne 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: \n\n SELECT A, B FROM Kind\n SELECT A, B, C FROM Kind\n\nHowever, if you always project properties `A`, `B`, and `C`, even when `C` is not required, only one index will be needed.\n\nConverting 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 \n\n SELECT * FROM Kind WHERE A \u003e 1 ORDER BY A, B\n\nwhich requires the index \n\n Index(Kind, A, B)\n\nConverting this to either of the projection queries \n\n SELECT C FROM Kind WHERE A \u003e 1 ORDER BY A, B\n SELECT A, B, C FROM Kind WHERE A \u003e 1 ORDER BY A, B\n\nintroduces a new property (`C`) and thus will require building a new index `Index(Kind,` `A,` `B,` `C)`. Note that the projection query \n\n SELECT A, B FROM Kind WHERE A \u003e 1 ORDER BY A, B\n\nwould *not* change the required index, since the projected properties `A` and `B` were already included in the existing query."]]