BigQuery 테이블은 데이터 블록으로 정리됩니다. TABLESAMPLE 절은 테이블에서 데이터 블록의 비율을 무작위로 선택하고 선택한 블록의 모든 행을 읽는 방식으로 작동합니다. 샘플링 세분화 수준은 데이터 블록 수로 제한됩니다.
일반적으로 BigQuery는 테이블 또는 테이블 파티션이 약 1GB보다 큰 경우 블록으로 분할합니다. 더 작은 테이블은 단일 데이터 블록으로 구성될 수 있습니다. 이 경우 TABLESAMPLE 절은 전체 테이블을 읽습니다. 샘플링 비율이 0보다 크고 테이블이 비어 있지 않으면 테이블 샘플링은 항상 일부 결과를 반환합니다.
블록은 크기가 다를 수 있으므로 샘플링되는 행의 정확한 비율은 다를 수 있습니다. 데이터 블록 대신 개별 행을 샘플링하려면 WHERE rand() < K 절을 대신 사용하면 됩니다. 하지만 이 방법을 사용하려면 BigQuery가 전체 테이블을 검색해야 합니다. 비용을 절감하면서도 행 수준 샘플링의 이점을 누리려면 두 기법을 모두 결합하면 됩니다.
다음 예시에서는 스토리지에서 데이터 블록의 약 20%를 읽은 다음 해당 블록에서 행의 10%를 무작위로 선택합니다.
SELECT * FROM dataset.my_table TABLESAMPLE SYSTEM (20 PERCENT)
WHERE rand() < 0.1
외부 테이블
파일 컬렉션에 데이터를 저장하는 외부 테이블에 TABLESAMPLE 절을 사용할 수 있습니다. BigQuery는 테이블이 참조하는 외부 파일의 하위 집합을 샘플링합니다. 일부 파일 형식의 경우 BigQuery는 개별 파일을 샘플링용 블록으로 분할할 수 있습니다. Google Sheets의 데이터와 같은 일부 외부 데이터는 하나의 데이터 블록으로 샘플링되는 단일 파일로 구성됩니다.
쓰기 최적화된 스토리지에서 샘플링
스트리밍 삽입과 함께 테이블 샘플링을 사용하는 경우 BigQuery가 쓰기 최적화된 스토리지에서 데이터를 샘플링합니다. 일부 경우에는 쓰기 최적화된 스토리지의 모든 데이터가 단일 블록으로 표시됩니다. 이 경우에는 쓰기 최적화된 스토리지의 모든 데이터가 결과에 표시되거나 전혀 표시되지 않습니다.
파티션을 나눈 테이블 및 클러스터링된 테이블
파티션 나누기 및 클러스터링은 특정 블록 내 모든 행이 동일한 파티션 나누기 키를 갖거나 종가를 가진 클러스터링 속성이 있는 블록을 생성합니다. 따라서 이러한 테이블의 샘플 세트는 파티션을 나누지 않고 클러스터링되지 않은 테이블의 샘플 집합보다 더 편향된 경향이 있습니다.
제한사항
샘플링된 테이블은 쿼리 문에 한 번만 나타날 수 있습니다. 이 제한사항에는 뷰 정의 내에서 참조되는 테이블이 포함됩니다.
뷰에서 데이터 샘플링은 지원되지 않습니다.
서브 쿼리 또는 테이블 값 함수 호출의 결과 샘플링은 지원되지 않습니다.
UNNEST 연산자 호출 결과와 같은 배열 스캔의 샘플링은 지원되지 않습니다.
IN 서브 쿼리 내부의 샘플링은 지원되지 않습니다.
행 수준 보안이 적용된 테이블에서 샘플링은 지원되지 않습니다.
테이블 샘플링 가격 책정
주문형 결제를 사용하는 경우 샘플링된 데이터를 읽는 데 요금이 부과됩니다. BigQuery는 TABLESAMPLE 절이 포함된 쿼리 결과를 캐시하지 않으므로 각 실행마다 스토리지에서 데이터를 읽는 비용이 발생합니다.
[[["이해하기 쉬움","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)"],[[["\u003cp\u003eTable sampling allows querying random subsets of data from large BigQuery tables, providing a representative sample without the cost of scanning the entire table.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eTABLESAMPLE\u003c/code\u003e clause, when used in a query, selects a specified percentage of data blocks from a table, returning all rows within those selected blocks.\u003c/p\u003e\n"],["\u003cp\u003eUnlike \u003ccode\u003eLIMIT\u003c/code\u003e, \u003ccode\u003eTABLESAMPLE\u003c/code\u003e returns a random selection, and results are not cached, which means subsequent queries may return different subsets of data.\u003c/p\u003e\n"],["\u003cp\u003eTable sampling can be combined with other SQL clauses like \u003ccode\u003eWHERE\u003c/code\u003e and \u003ccode\u003eJOIN\u003c/code\u003e, as well as with parameterized queries to dynamically adjust the sampling percentage.\u003c/p\u003e\n"],["\u003cp\u003eTable sampling is supported for external tables, but may behave differently based on file type or structure, and it's subject to several limitations, including restrictions on views, subqueries, array scans, and row-level security.\u003c/p\u003e\n"]]],[],null,["# Table sampling\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\nTable sampling lets you query random subsets of data from large\nBigQuery tables. Sampling returns a variety of records while avoiding\nthe costs associated with scanning and processing an entire table.\n\nUsing table sampling\n--------------------\n\nTo use table sampling in a query, include the\n[`TABLESAMPLE`](/bigquery/docs/reference/standard-sql/query-syntax#tablesample_operator)\nclause. For example, the following query selects approximately 10% of a table's\ndata: \n\n SELECT * FROM dataset.my_table TABLESAMPLE SYSTEM (10 PERCENT)\n\nUnlike the `LIMIT` clause, `TABLESAMPLE` returns a random subset of data from a\ntable. Also, BigQuery does not cache the results of queries that\ninclude a `TABLESAMPLE` clause, so the query might return different results each\ntime.\n\nYou can combine the `TABLESAMPLE` clause with other selection conditions. The\nfollowing example samples about 50% of the table and then applies a `WHERE`\nclause: \n\n SELECT *\n FROM dataset.my_table TABLESAMPLE SYSTEM (50 PERCENT)\n WHERE customer_id = 1\n\nThe next example combines a `TABLESAMPLE` clause with a `JOIN` clause: \n\n SELECT *\n FROM dataset.table1 T1 TABLESAMPLE SYSTEM (10 PERCENT)\n JOIN dataset.table2 T2 TABLESAMPLE SYSTEM (20 PERCENT) USING (customer_id)\n\nFor smaller tables, if you join two samples and none of the sampled rows meet\nthe join condition, then you might receive an empty result.\n\nYou can specify the percentage as a\n[query parameter](/bigquery/docs/parameterized-queries). The next example shows\nhow to pass the percentage to a query by using the bq command-line tool: \n\n bq query --use_legacy_sql=false --parameter=percent:INT64:29 \\\n 'SELECT * FROM `dataset.my_table` TABLESAMPLE SYSTEM (@percent PERCENT)`\n\nBigQuery tables are organized into data blocks. The `TABLESAMPLE`\nclause works by randomly selecting a percentage of data blocks from the table\nand reading all of the rows in the selected blocks. The sampling granularity\nis limited by the number of data blocks.\n\nTypically, BigQuery splits tables or table partitions into blocks\nif they are larger than about 1 GB. Smaller tables might consist of a single\ndata block. In that case, the `TABLESAMPLE` clause reads the entire table. If\nthe sampling percentage is greater than zero and the table is not empty, then\ntable sampling always returns some results.\n\nBlocks can be different sizes, so the exact fraction of rows that are sampled\nmight vary. If you want to sample individual rows, rather than data blocks, then\nyou can use a `WHERE rand() \u003c K` clause instead. However, this approach requires\nBigQuery to scan the entire table. To save costs but still\nbenefit from row-level sampling, you can combine both techniques.\n\nThe following example reads approximately 20% of the data blocks from storage\nand then randomly selects 10% of the rows in those blocks: \n\n SELECT * FROM dataset.my_table TABLESAMPLE SYSTEM (20 PERCENT)\n WHERE rand() \u003c 0.1\n\nExternal tables\n---------------\n\nYou can use the `TABLESAMPLE` clause with external tables that store data in a\ncollection of files. BigQuery samples a subset of the external\nfiles that the table references. For some file formats, BigQuery\ncan split individual files into blocks for sampling. Some external data, such as\ndata in Google Sheets, consists of a single file that is sampled as one block\nof data.\n\nSampling from the write-optimized storage\n-----------------------------------------\n\nIf you use table sampling with\n[streaming inserts](/bigquery/docs/streaming-data-into-bigquery), then\nBigQuery samples data from the write-optimized storage. In some cases,\nall the data in the write-optimized storage is represented as a single block. When that happens,\neither all the data in the write-optimized storage appears in the results, or none of it does.\n\nPartitioned and clustered tables\n--------------------------------\n\nPartitioning and clustering produce blocks where all rows within a specific\nblock have either the same partitioning key or have clustering attributes with\nclose values. Therefore, sample sets from these tables tend to be more biased\nthan sample sets from non-partitioned, non-clustered tables.\n\nLimitations\n-----------\n\n- A sampled table can only appear once in a query statement. This restriction includes tables that are referenced inside view definitions.\n- Sampling data from views is not supported.\n- Sampling the results of subqueries or table-valued function calls is not supported.\n- Sampling from an array scan, such as the result of calling the `UNNEST` operator, is not supported.\n- Sampling inside an `IN` subquery is not supported.\n- Sampling from tables with row-level security applied is not supported.\n\nTable sampling pricing\n----------------------\n\nIf you use [on-demand billing](/bigquery/pricing#on_demand_pricing), then you\nare charged for reading the data that is sampled. BigQuery does\nnot cache the results of a query that includes a `TABLESAMPLE` clause, so each\nexecution incurs the cost of reading the data from storage."]]