All Products
Search
Document Center

PolarDB:ST_IsRandomSampled

Last Updated:Mar 28, 2026

Determines whether a record is included in a random sample based on the provided properties and sample rate. Returns true with a probability equal to the sample_rate value.

Syntax

BOOLEAN ST_IsRandomSampled(RECORD tuple, INTEGER sample_rate)

Parameters

ParameterDescription
tupleThe properties used to generate a hash value for the record. The hash value determines whether the record is sampled. Use ROW() to construct the tuple from one or more columns. For accurate sampling results, select properties or property combinations that are unique and evenly distributed.
sample_rateThe percentage of records to sample, expressed as an integer. For example, a value of 50 samples 50% of records.

Description

  • Returns true if the record is sampled; returns false otherwise.

  • The return value is calculated based on the values of tuple and sample_rate.

  • When applied to all records in a table, the tuple value must be unique per record. This ensures the actual number of sampled records is as close as possible to sample_rate% of the total row count.

Examples

-- Return true for approximately half of the records, false for the other half.
SELECT ST_IsRandomSampled(ROW(id), 50) FROM table;

-- Calculate the area of geometries that fall in an 80% sample.
SELECT ST_Area(geom) FROM table WHERE ST_IsRandomSampled(ROW(geom), 80);

-- Sample half of the geometries and convert them to MVTGeom objects.
SELECT ST_AsMVTGeom(geom, ST_Transform(ST_TileEnvelope(0,0,0),4326)) FROM table
WHERE ST_IsRandomSampled(ROW(geom), 50) AND geom&&ST_Transform(ST_TileEnvelope(0,0,0),4326));