All Products
Search
Document Center

ApsaraDB RDS:ST_IsRandomSampled

Last Updated:Oct 08, 2023

This topic describes the ST_IsRandomSampled function. This function checks whether a record is sampled based on the provided property values and sample rate and returns a BOOLEAN value. The probability that the record is sampled is the value of the sample_rate parameter.

Syntax

BOOLEAN ST_IsRandomSampled(RECORD tuple, INTEGER sample_rate)

Parameters

Parameter

Description

tuple

The properties that are used to generate the hash value for a record. The system determines whether the record is sampled based on the hash value. You can use ROW() to generate a hash value based on different properties of a record. We recommend that the selected properties or property sets are unique and evenly distributed.

sample_rate

The sample rate. This parameter specifies the probability that a record is sampled. For example, if you set the sample_rate parameter to 50, the probability that a record is sampled is 50%, and the ST_IsRandomSampled function returns true for records that have a 50% probability of being sampled.

Description

  • If a record is sampled, true is returned. Otherwise, false is returned.

  • The probability that a record is sampled is specified by the sample_rate parameter.

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

  • When this function is called for all data records in a table, the value of the tuple parameter must be unique. This ensures that the number of sampled data records is as close as possible to the product of the sample_rate parameter value and the total number of records in the table.

Examples

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

-- Calculate the area of the geometry that has a 80% possibility of being sampled.
SELECT ST_Area(geom) FROM table WHERE ST_IsRandomSampled(ROW(geom), 80);

-- Randomly select half of the geometries and convert them into the 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));