Updated on 2025-03-13 GMT+08:00

Sample Scan

Description

Sample scan is a method of scanning tables. It can randomly extract some data from a table for query instead of scanning the entire table. Sample scan is used together with the TABLESAMPLE keyword in [ TABLESAMPLE sampling_method ( argument [, ...] ) [ REPEATABLE ( seed ) ] ] mode. This technology is usually used in a query scenario in which a large table or random sampling is required, to improve query efficiency.

Typical Scenarios

When the tablesample syntax is used for query, sampling_method is set to system or bernoulli.

Examples

Example: Query the sampling data.

-- Prepare data.
gaussdb=# CREATE TABLE t1 (c1 number, c2 number, c3 number); 
CREATE TABLE 
gaussdb=# INSERT INTO t1 VALUES (generate_series(1,100), 2, 3); 
INSERT 0 100

-- Execution result.
gaussdb=# EXPLAIN SELECT c1 FROM t1 tablesample system(10); 
                     QUERY PLAN                      
---------------------------------------------------- 
 Sample Scan on t1  (cost=0.00..272.00 rows=10000 width=6)
   Sampling: system (10::real)
(2 rows)

-- Drop.
gaussdb=# DROP TABLE t1;

In the preceding example, the output of the sample scan operator is as follows.

Item

Description

Sample Scan

Operator name.

Sampling

Operator sampling mode. In the example, system is used as the sampling method, and the sampling ratio is 10%.