TABLESAMPLE BERNOULLI And SYSTEM Sampling

TABLESAMPLE BERNOULLI flips a coin per row while SYSTEM samples whole pages; REPEATABLE seeds both so repeated runs return the exact same rows.

SqlData Fetching

Code

snippet
-- BERNOULLI:逐行掷硬币,独立按概率抽行,分布均匀
SELECT id, region
FROM events TABLESAMPLE BERNOULLI (10) REPEATABLE (42);

-- SYSTEM:按数据页整块抽,更快,但抽样单位是页,行可能聚集
SELECT id, region
FROM events TABLESAMPLE SYSTEM (10) REPEATABLE (42);

-- REPEATABLE (seed):同一种子重复执行返回完全相同的行
SELECT count(*) AS sampled_rows
FROM events TABLESAMPLE BERNOULLI (50) REPEATABLE (7);

-- WHERE 在抽样之后执行:先抽 10%,再从样本里过滤大额行
SELECT id, region
FROM events TABLESAMPLE BERNOULLI (10) REPEATABLE (42)
WHERE amount_cents > 100000;

-- 百分比是"大约":两种方式都不保证精确行数

How the snippet works

What runs here is SELECT — events are the objects involved.

What the statements actually assemble: a WHERE clause that keeps only the rows a statement is meant to touch; COUNT aggregation.

Applying it

Point it at the events table.

Statements that matter

SELECT id, region
FROM events TABLESAMPLE BERNOULLI (10) REPEATABLE (42)
WHERE amount_cents > 100000;
SELECT count(*) AS sampled_rows
FROM events TABLESAMPLE BERNOULLI (50) REPEATABLE (7);
SELECT id, region
FROM events TABLESAMPLE BERNOULLI (10) REPEATABLE (42);

Interactive Tools for This Topic

FAQ

What objects does the SQL involve?
`events` — referenced directly, no join needed.