All Products
Search
Document Center

ApsaraDB RDS:ST_IsRandomSampled

Last Updated:Mar 28, 2026

Checks whether a record is sampled based on its hash value and the specified sample rate. Returns true if the record is sampled, false otherwise. The sampling probability equals the value of sample_rate.

Syntax

BOOLEAN ST_IsRandomSampled(RECORD tuple, INTEGER sample_rate)

Parameters

ParameterDescription
tupleThe record properties used to generate the hash value. The system determines whether to sample a record based on this hash. Use ROW() to build a hash from multiple properties. For accurate sampling ratios across a table, select properties or property combinations that are unique and evenly distributed.
sample_rateThe sampling probability, expressed as an integer percentage. For example, a value of 50 means each record has a 50% chance of being sampled.

Description

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

  • The sampling probability is controlled by sample_rate.

  • The return value is determined by both tuple and sample_rate.

  • When applied to all records in a table, tuple must be unique per record. This ensures the number of sampled records approximates sample_rate × total record count.

Examples

-- Return results for all records. true is returned for half of the records, and false for the other half.
SELECT ST_IsRandomSampled(ROW(id), 50) FROM table;

-- Calculate the area of geometries that have an 80% probability of being sampled.
SELECT ST_Area(geom) FROM table WHERE ST_IsRandomSampled(ROW(geom), 80);

-- Randomly select 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));