[[["易于理解","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-09-04。"],[],[],null,["# Structured row key queries\n==========================\n\n|\n| **Preview**\n|\n|\n| This product or feature is subject to the \"Pre-GA Offerings Terms\" in the General Service Terms section\n| of the [Service Specific Terms](/terms/service-terms#1).\n|\n| Pre-GA products and features are available \"as is\" and might have limited support.\n|\n| For more information, see the\n| [launch stage descriptions](/products#product-launch-stages).\n\nIf a table has a row key schema that defines *structured row keys*, you can use\nSQL to query the individual segments -- or columns -- of the row key.\n\nContinuous materialized views, including asynchronous secondary indexes,\ngenerate structured row keys by default. You can also define a structured row key\nfor any Bigtable table that you want to query with SQL by creating\na *row key schema* for the table. For more information, see\n[Manage row key schemas](/bigtable/docs/manage-row-key-schemas).\n\nKeys\n----\n\nIn a Bigtable table without a structured row key schema, each row\nis indexed by a single row key. When you query the table with SQL, the row key\nis a column named `_key` that SQL uses as the primary key. It's not a\npseudocolumn, so values in the `_key` column are returned when you execute a\n`SELECT *` query on the table.\n\nOn the other hand, in a table that has a row key schema, the row key (primary\nkey) is the combination of all the columns named as fields in the row key\nschema. When you add a row key schema to a table, the `_key` column becomes a\npseudocolumn, which means it won't show up in a `SELECT *` query, but you can\nselect it explicitly with a `SELECT _key` statement.\n| **Important:** If any row keys in a table don't conform to the row key schema, queries that select structured row key columns fail and return an error.\n\nSample queries\n--------------\n\nThe examples in this section assume that a table named `sales` has the\nfollowing schema: \n\n field {\n field_name: \"user_id\"\n type: { bytes_type { encoding { raw {} } } }\n }\n field {\n field_name: \"purchase_date\"\n type: { string_type { encoding { utf8_bytes {} } } }\n }\n field {\n field_name: \"order_number\"\n type: { string_type { encoding { utf8_bytes {} } } }\n }\n encoding {\n delimited_bytes { delimiter \"#\" }\n }\n\nThe `sales` table contains the following data. The `product` column family has\ntwo columns.\n\n### Structured row key query results\n\nBecause the `sales` table has structured row keys, if you query the table with a\n`SELECT *` statement, the query returns each segment of the row key as a\nseparate column. As in any SQL query to a Bigtable table, columns\nin a column family are expressed as maps. \n\n SELECT * from sales\n\nResults look like the following:\n\nYou can also specify the row key columns in your query, as shown in the\nfollowing example: \n\n SELECT\n product[product_type] AS product_type,\n product[product_name] AS product_name\n FROM sales\n WHERE user_id = b\"user1\"\n\nThe results look like the following:\n\n### Filters\n\nYou can filter on the row key schema columns using SQL functions. The following\nexample assumes that `CURRENT_DATE()` returns `2025-05-24`: \n\n SELECT\n user_id,\n product[\"product_name\"] AS product_name\n FROM\n sales\n WHERE\n PARSE_DATE(\"YYYY-MM-DD\", purchase_date) = CURRENT_DATE()\n AND user_id = b\"user2\"\n\nThe results are as follows:\n\n### Aggregate queries\n\nThe following example shows how use an aggregate query on structured row key\nfields: \n\n SELECT\n user_id,\n product[product_type] AS product_type,\n count(*) AS count\n FROM sales\n GROUP BY 1, 2\n\nThe query results are as follows:\n\n### Original row key\n\nTo retrieve the original row key in a table with structured row keys, specify\nthe `_key` column in your query.\n| **Note:** This query works for a continuous materialized view only if `_key` was selected in the [query that defines the\nview](/bigtable/docs/continuous-materialized-view-queries). \n\n SELECT\n _key, user_id\n FROM sales\n\nThe query returns the following:\n\nWhat's next\n-----------\n\n- [GoogleSQL for Bigtable overview](/bigtable/docs/googlesql-overview)\n- [Schema design best practices](/bigtable/docs/schema-design)"]]