TABLESAMPLE clause, attached directly to a table in FROM. Use it to
explore unfamiliar data, run a smaller experiment, or estimate aggregate
statistics without reading every row. For a random subset through the
Python SDK instead — in Embedded or Enterprise — see
Permutation. To select specific, known rows
rather than a random fraction, see Selective Sampling.
Example data
Connect with an Enterprise SQL client. The examples use a small document table and a separate label table. Expand the setup below to create them.Create the example tables
Create the example tables
curation_documents has six records describing four documents.
curation_labels has one label for each of three documents. row_id identifies
a record; doc_id identifies the document it describes.Run each statement separately in a scratch database or namespace. Create these
tables once for the whole section, using fresh names if they already exist.score is a sample quality score, not a search result score. Document 10 has
two versions. Document 30 has two records at the same version. Document 40
has no label. These cases let you check the effect of each operation.Basic sampling
A percentage selects a fraction of the source;ROWS requests a row count:
TABLESAMPLE requires a table that is not WAL-backed. See the
sampling reference for this restriction; do not
change a production table’s write configuration just to try an example.
Sampling methods
Choose a method according to how records should be selected:Repeatable samples
A seed requests a repeatable draw:Sampling with joins
Sampling both input tables independently changes which pairs can match. For a one-to-one match, independent 10% row samples retain a given pair with about 1% probability, not 10%. To sample documents while retaining their available labels, sample only the document table:TABLESAMPLE cannot be applied directly to a subquery result.
Apply a Function to a sample
Sample first, then call a Function only on the rows you drew — useful for trying an expensive Function on a subset before running it over the whole table. Registercaption_quality first: