TOKENIZE_JSON 함수를 사용하여 GoogleSQL에서 JSON 색인을 만들거나 TOKENIZE_JSONB 함수를 사용하여 PostgreSQL에서 JSONB 색인을 만들 수 있습니다. 자세한 내용은 TOKENIZE_JSON 및 TOKENIZE_JSONB를 참조하세요.
JSON 및 JSONB 쿼리
검색 색인을 사용하여 JSON 격리 및 키 존재 조건이 포함된 쿼리를 가속화할 수 있습니다. JSON 격리는 JSON 문서 하나가 다른 JSON 문서 내에 포함되어 있는지 확인합니다. 키 존재는 키가 데이터베이스 스키마에 있는지 여부를 확인합니다.
필드 액세스, 배열 아래 첨자 연산자, IS NOT NULL을 사용하여 키 존재 조건을 구성합니다. 필드 액세스 및 배열 아래 첨자 연산자는 JSON 문서 경로를 설명합니다. IS NOT NULL은 이 경로 존재를 확인합니다(예: doc.sub.path[@index].key IS NOT NULL).
PostgreSQL:
@> 및 <@ 연산자를 사용하여 JSONB 격리를 표현합니다. 자세한 내용은 JSONB 연산자를 참조하세요.
?, ?|, ?& 연산자를 사용하여 키 존재 조건을 구성합니다.
자세한 내용은 JSONB 연산자를 참조하세요.
쿼리에서 검색 색인에 모든 유형의 JSON 조건을 여러 개 포함할 수 있습니다. AND, OR, NOT을 사용하여 JSON 조건을 논리적 조합에 포함할 수도 있습니다.
검색 색인 사용 확인
쿼리에서 검색 색인을 사용하는지 확인하려면 쿼리 실행 계획에서 검색 색인 스캔 노드를 찾습니다.
제한사항
JSON 및 JSONB 검색 색인을 포함한 검색 색인은 읽기 전용 트랜잭션에서만 사용됩니다. Spanner는 읽기-쓰기 트랜잭션에서 관련 보조 색인을 사용할 수 있습니다. 읽기-쓰기 트랜잭션에서 검색 색인을 강제로 사용하려고 하면 다음 오류가 발생합니다.
ERROR: spanner: code = "InvalidArgument", desc = "The search index
AlbumsIndex cannot be used in transactional queries by default."
특정 대규모 또는 매우 복잡한 JSON 문서를 검색 색인에 저장하려고 하면 too many search token bytes 오류가 반환될 수 있습니다. 이 JSON 문서의 출력 토큰 크기는 10MB 미만이어야 합니다. 전체 문서를 검색할 필요가 없으면 대신 문서의 더 작은 하위 집합을 추출하고(예: 생성된 열 사용) 열을 검색하는 것이 좋습니다.
[[["이해하기 쉬움","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-05(UTC)"],[],[],null,["# JSON search indexes\n\n| **Note:** This feature is available with the Spanner Enterprise edition and Enterprise Plus edition. For more information, see the [Spanner editions overview](/spanner/docs/editions-overview).\n\n\u003cbr /\u003e\n\nIn addition to indexing text, the Spanner [search index](/spanner/docs/full-text-search/search-indexes)\nprovides an efficient way to index and query JSON and JSONB documents. Use\nsearch indexes for standalone JSON and JSONB queries, or to augment other\n[full-text search](/spanner/docs/full-text-search) queries.\n\nFor more information, see [Index JSON data](/spanner/docs/working-with-json#index) and\n[Index JSONB data](/spanner/docs/working-with-jsonb#index).\n\nTokenize JSON and JSONB\n-----------------------\n\nYou can use the `TOKENIZE_JSON` function to create a JSON index in\nGoogleSQL, or the `TOKENIZE_JSONB` function to create a JSONB index in\nPostgreSQL. For details, see [`TOKENIZE_JSON`](/spanner/docs/reference/standard-sql/search_functions#tokenize_json)\nand [`TOKENIZE_JSONB`](/spanner/docs/reference/postgresql/functions-and-operators#indexing).\n\nJSON and JSONB queries\n----------------------\n\nYou can use a search index to accelerate queries that include\n*JSON containment* and *key existence* conditions. JSON containment determines\nif one JSON document is contained within another. Key existence determines if a\nkey exists in the database schema.\n\n- In GoogleSQL:\n\n - Express JSON containment in your schema by using the [`JSON_CONTAINS`](/spanner/docs/reference/standard-sql/json_functions#json_contains) function.\n - Construct key existence conditions using the field access, array subscript operators, and `IS NOT NULL`. The field access and array subscript operators describe a JSON document path. `IS NOT NULL` checks for the existence of this path (for example, `doc.sub.path[@index].key IS NOT NULL`).\n- In PostgreSQL:\n\n - Express JSONB containment using the `@\u003e` and `\u003c@` operators. For more information, see [JSONB operators](/spanner/docs/reference/postgresql/functions-and-operators#jsonb_operators).\n - Construct key existence conditions using the `?`, `?|`, and `?&` operators. For more information, see [JSONB operators](/spanner/docs/reference/postgresql/functions-and-operators#jsonb_operators).\n\nIn your queries, you can include multiple JSON conditions of any type in the\nsearch index. You can also include the JSON conditions in a logical combination\nusing `AND`, `OR`, and `NOT`.\n\n### Check search index usage\n\nTo check that that your query uses a search index, look for a\n*Search index scan* node in the [query execution plan](/spanner/docs/query-execution-plans).\n\nRestrictions\n------------\n\n- Search indexes, including JSON and JSONB search indexes, are used only in read-only transactions. Spanner might use relevant secondary indexes in a read-write transaction. If you attempt to force the use of a search index in a read-write transaction, the following error occurs: `ERROR: spanner: code = \"InvalidArgument\", desc = \"The search index\n AlbumsIndex cannot be used in transactional queries by default.\"`\n- Attempts to store certain large or very complex JSON documents in a search index might return a `too many search token bytes` error. The output token size from this JSON document must be smaller than 10 MB. If you don't need the entire document to be searchable, consider extracting a smaller subset of the document (for example, by using a generated column) and searching over the column instead.\n\nWhat's next\n-----------\n\n- Learn about [tokenization and tokenizers](/spanner/docs/full-text-search/tokenization).\n- Learn about [search indexes](/spanner/docs/full-text-search/search-indexes).\n- Learn about [indexing JSON data](/spanner/docs/working-with-json#index).\n- Learn about [indexing JSONB data](/spanner/docs/working-with-jsonb#index)."]]