Lesson 2 of 5 Data Quality

Choosing Sampling Methods for Profiling and Validation

This lesson explains how a well-chosen sample gives reliable statistics quickly when profiling or validating every row of a huge dataset is slow and expensive, provided the method does not bias the result.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish random, stratified and systematic sampling.
  • Sample data with Athena TABLESAMPLE and Spark.
  • Recognise checks that require the full dataset.

The methods

Sampling examines part of a dataset to profile or test it quickly, and the method decides whether the sample is representative. Random sampling gives every row an equal chance and is the general-purpose choice. Stratified sampling samples within groups, such as each country, so that small groups are not missed and each keeps its proportion. Systematic sampling takes every nth row, which is simple but biased if the data repeats in a pattern that matches the interval.

First-N sampling takes the first rows, which is fast but biased when data is sorted or the earliest records differ from later ones. Reservoir sampling keeps a fixed-size random sample from a stream whose length is unknown, replacing items as new ones arrive. The following table summarises the methods.

MethodHow it worksNote
RandomEvery row has an equal chanceGeneral purpose
StratifiedSamples within groups, such as per countryPreserves each group's proportions
SystematicEvery nth rowSimple; biased if the data has a repeating pattern
First-NThe first N rowsFast but biased towards early or sorted data
ReservoirKeeps a fixed-size sample from a streamFor streams of unknown length

The following diagram compares the rows per country in a random sample and a stratified sample of an address table.

Rows per country in two samples of the address table, where one country has 90% of the rows. A random 0.1% sample is accurate overall but has too few rows from the smallest countries to estimate their invalid postcodes. A stratified sample of 100,000 rows per country, taken with Spark's sampleBy, gives reliable estimates for every country.
Bar heights are not to scale; the counts are approximate.
Match the pairs

Sampling methods

Exam skills 3.4.4

Match each requirement with the sampling method.

Log in to try this activity Interactive activities are free with an account.

Sampling on AWS

Several AWS services support sampling. In Amazon Athena, TABLESAMPLE BERNOULLI includes each row with a given probability, giving an even, row-level sample, while TABLESAMPLE SYSTEM selects whole blocks of data, which is faster because it reads less but is coarser, because rows within a block tend to be similar.

AWS Glue DataBrew projects work on a sample, configured as the first rows, the last rows or a random selection, so interactive profiling and recipe building stay fast on large datasets. In Apache Spark, sample() takes a random sample, and sampleBy() takes a stratified sample with a different fraction for each value of a column, which ensures that rare categories are still represented.

True or false

Sampling tools

Exam skills 3.4.4

Decide whether each statement about sampling on AWS is true.

Log in to try this activity Interactive activities are free with an account.

When not to sample

Samples estimate statistics; they cannot prove the absence of a problem. Checks for uniqueness, referential integrity, or rules that every row must satisfy need the full dataset. A common approach profiles a sample to choose rules, then runs the rules on all data in the pipeline.

Sort into groups

Sample or full data?

Exam skills 3.4.4 · 3.4.1

Decide whether each check can use a sample.

Log in to try this activity Interactive activities are free with an account.
Worked example

A sample that keeps every country

A profiling job must estimate the share of invalid postcodes in a 4-billion-row address table. 90% of rows are from one country, and a simple random sample of 0.1% would contain very few rows from the smallest countries. The following table compares the methods.

MethodResult
First 1 million rowsBiased: the table is sorted by country
Random 0.1%Accurate overall, unreliable for small countries
Stratified: 100,000 rows per countryReliable estimates for every country

The team uses Spark's sampleBy with a fraction per country. For the rule that every postcode must be valid before publishing, however, the pipeline checks all rows.

Check yourself

Which sampling method suits a stream whose size is unknown?

Show answer

Reservoir sampling.

Exam Tips

Remember for the exam

  • For a sample that keeps the proportions of each category, use stratified sampling.
  • To sample a stream of unknown size, use reservoir sampling.
  • First-N sampling is biased towards early or sorted data.
  • In Athena, use TABLESAMPLE BERNOULLI or SYSTEM.
  • Uniqueness and integrity checks need the full dataset, not a sample.