다음 예에서는 다양한 GoogleSQL 쿼리를 사용하여 비동기 보조 색인을 만드는 방법을 보여줍니다.
열 한정자 및 값을 기반으로 역색인 만들기
다음 쿼리는 유연한 스키마가 있는 column family에서 모든 키-값 쌍을 추출하여 역색인을 만듭니다. MAP_ENTRIES를 사용하여 stats_summary 열 패밀리가 저장하는 모든 데이터의 배열을 가져온 다음 UNNEST를 사용하여 각 키-값 쌍을 별도의 행으로 변환합니다. 결과 색인을 사용하면 stat_description 및 stat_value에 기반한 빠른 조회가 가능합니다. 소스 테이블에 새로운 유형의 통계를 추가하면 Bigtable이 비동기 색인에 이를 자동으로 포함합니다.
다음 예에서는 cell_plan column family에 저장된 데이터 요금제를 분류하여 새 색인 키 total_plan_capacity를 만듭니다. 이 쿼리는 MAP_KEYS를 사용하여 cell_plan의 모든 키를 가져오고 ARRAY_INCLUDES_ANY를 사용하여 특정 데이터 계획 키를 필터링합니다. 그런 다음 CASE 문은 이러한 데이터 요금제의 존재 여부와 값을 기반으로 total_plan_capacity 카테고리를 할당하는 로직을 정의합니다. 이를 통해 결합된 데이터 요금제 용량을 기반으로 효율적인 조회가 가능합니다.
Bigtable은 각 셀 값과 연결된 타임스탬프를 저장합니다. FROM 절에서 WITH_HISTORY=>TRUE 플래그를 사용하면 모든 버전의 데이터와 타임스탬프를 가져올 수 있습니다. 그런 다음 UNPACK 테이블 함수는 각 타임스탬프 값이 별도의 _timestamp 열이 있는 자체 행에 표시되도록 쿼리 결과를 확장합니다. 이렇게 하면 새 행 키가 이러한 타임스탬프 값을 기반으로 하는 비동기 보조 색인을 만들어 시간 범위를 기반으로 하는 빠른 조회를 지원할 수 있습니다.
Bigtable은 JSON과 같은 형식의 데이터를 비롯한 대규모 워크로드의 키-값 저장소로 자주 사용됩니다. 다음 예에서는 JSON이 저장하는 데이터를 빠르게 조회하기 위해 비동기 보조 색인을 만드는 방법을 보여줍니다. 이 쿼리는 JSON_EXTRACT를 사용하여 sales라는 테이블의 transaction_information column family 내에 있는 JSON 문자열에서 store_id을 추출합니다. 이 색인을 사용하면 특정 매장 ID별로 판매 거래를 빠르게 검색할 수 있습니다.
[[["이해하기 쉬움","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(UTC)"],[],[],null,["Asynchronous secondary index queries\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\nThis document provides examples of common query patterns for building\nasynchronous secondary indexes in Bigtable. The examples use IDs and\nvalues that are similar to those in\n[Data for examples](/bigtable/docs/using-filters#data) and you can\n[create a test table](/bigtable/docs/creating-test-table#create-test_table) to\ntest the queries.\n\nBefore you read this page, familiarize yourself with\n[Create an asynchronous secondary index](/bigtable/docs/asynchronous-secondary-index)\nand\n[GoogleSQL for Bigtable](/bigtable/docs/googlesql-overview).\n\nExample queries\n\nThe following examples show how to create asynchronous secondary indexes using\ndifferent GoogleSQL queries.\n\nCreate an inverted index based on a column qualifier and values\n\nThe following query creates an inverted index by extracting all key-value pairs\nfrom a column family that has a flexible schema. It uses `MAP_ENTRIES` to get an\narray of all data that the `stats_summary` column family stores, and then\n`UNNEST` to transform each key-value pair into a separate row. The resulting\nindex allows for fast lookups based on `stat_description` and `stat_value`. If\nyou add new types of statistics to the source table, then Bigtable\nincludes them in the asynchronous index automatically. \n\n SELECT\n stats.key as stat_description,\n stats.value as stat_value,\n _key as original_key,\n cell_plan as cell_plan\n FROM test_table\n CROSS JOIN UNNEST(MAP_ENTRIES(stats_summary)) stats\n ORDER BY stat_description, stat_value, original_key\n\nCreate a new row key based on existing data\n\nThe following example creates a new index key, `total_plan_capacity`, by\ncategorizing data plans that the `cell_plan` column family stores. The query\nuses `MAP_KEYS` to get all keys in `cell_plan` and `ARRAY_INCLUDES_ANY` to\nfilter for specific data plan keys. A `CASE` statement then defines the logic to\nassign a `total_plan_capacity` category that's based on the presence and values\nof these data plans. This allows for efficient lookups based on the combined\ndata plan capacity. \n\n SELECT\n CASE\n WHEN\n cell_plan['data_plan_01gb'] = \"true\"\n AND (cell_plan['data_plan_05gb'] = \"false\" OR cell_plan['data_plan_05gb'] IS NULL)\n AND (cell_plan['data_plan_10gb'] = \"false\" OR cell_plan['data_plan_10gb'] IS NULL)\n THEN 'x-small'\n\n WHEN\n cell_plan['data_plan_01gb'] = \"true\"\n AND (cell_plan['data_plan_05gb'] = \"true\")\n AND (cell_plan['data_plan_10gb'] = \"false\" OR cell_plan['data_plan_10gb'] IS NULL)\n THEN 'small'\n\n WHEN\n cell_plan['data_plan_01gb'] = \"true\"\n AND (cell_plan['data_plan_05gb'] = \"false\" OR cell_plan['data_plan_05gb'] IS NULL)\n AND (cell_plan['data_plan_10gb'] = \"true\")\n THEN 'medium'\n\n WHEN\n (cell_plan['data_plan_01gb'] = \"false\" OR cell_plan['data_plan_01gb'] IS NULL)\n AND (cell_plan['data_plan_05gb'] = \"true\")\n AND (cell_plan['data_plan_10gb'] = \"true\")\n THEN 'large'\n\n WHEN\n cell_plan['data_plan_01gb'] = \"true\"\n AND (cell_plan['data_plan_05gb'] = \"true\")\n AND (cell_plan['data_plan_10gb'] = \"true\")\n THEN 'x-large'\n END as total_plan_capacity,\n _key as original_key,\n stats_summary\n FROM test_table\n WHERE ARRAY_INCLUDES_ANY(MAP_KEYS(cell_plan), [\"data_plan_01gb\", \"data_plan_05gb\",\"data_plan_10gb\"])\n ORDER BY total_plan_capacity, original_key\n\nCreate a time-series-based index\n\nBigtable stores each cell value and its associated timestamp. By\nusing the `WITH_HISTORY=\u003eTRUE` flag in the `FROM` clause, you can retrieve all\nversions of data and their timestamps. The `UNPACK` table function then expands\nthe query results so that each timestamped value appears on its own row with a\nseparate `_timestamp` column. This lets you create an asynchronous secondary\nindex where the new row key is based on these timestamp values, enabling quick\nlookups that are based on time ranges. \n\n SELECT\n _timestamp as stats_timestamp,\n _key as original_key,\n stats_summary\n FROM UNPACK((\n select\n _key,\n stats_summary\n FROM\n test_table (WITH_HISTORY=\u003eTRUE)\n ))\n ORDER BY stats_timestamp, original_key\n\nCreate an index entry for items and values in a JSON cell\n\nBigtable is often used as a key-value store for large-scale\nworkloads, including data in formats such as JSON. The following example shows\nhow to create an asynchronous secondary index for fast lookups of data that JSON\nstores. The query uses `JSON_EXTRACT` to extract the `store_id` from a JSON\nstring within the `transaction_information` column family in a table named\n`sales`. This index allows for quick retrieval of sales transactions by specific\nstore IDs. \n\n SELECT\n JSON_EXTRACT(transaction_information[\"json\"], \"$.store_id\") as store_id,\n _key as original_key,\n transaction_information\n from sales\n ORDER BY store_id, original_key;\n\nWhat's next\n\n- [Continuous materialized view queries](/bigtable/docs/continuous-materialized-view-queries)\n- [Create and manage continuous materialized views](/bigtable/docs/manage-continuous-materialized-views)"]]