Snowflake SQL Tutorial 61 - SAMPLE in Snowflake

Snowflake SQL · Episode 61

In this post we are going to cover SAMPLE in Snowflake.

SAMPLE gives us back a random part of a table. We write it in the FROM clause, right after the table name, with a number in brackets. The number is a percentage, so we are asking Snowflake for roughly that much of the table and letting it choose which rows.

We will start by running the same sample twice and watching it give two different answers. Then we will find out why, make it repeatable with SEED, look at the two sampling methods, ask for a fixed number of rows instead of a percentage, and finish with the two places a sample will quietly mislead us.

Every query below was run on a real Snowflake account, and every result you see is the output it gave back. The grids that have no SEED are single draws of a random process, so yours will not match them, and that is the whole subject of the post.

Episode 61 of the Snowflake SQL: Zero to Hero series on TechBrothersIT.

Let's create some sample data

Everything below samples one table, ORDERS. The script below is the shared setup for this course. Run it once.

The table is deliberately small, because a small table makes the behaviour this post is about impossible to miss.

Sample data — run once
-- ORDERS and CUSTOMERS come from the setup script this course uses
-- throughout. Run it once; every episode reuses the same tables.
USE DATABASE DEMO_DB;
USE SCHEMA PUBLIC;

-- Nothing on this page modifies ORDERS. That matters more than usual here:
-- a seeded sample only keeps returning the same rows while the table it
-- reads is unchanged.
SELECT COUNT(*) AS N_ORDERS,
       COUNT(DISTINCT CUSTOMER_ID) AS CUSTOMERS,
       SUM(AMOUNT) AS TOTAL
  FROM ORDERS;

Let's count what we have to work with.

Query — the population
SELECT COUNT(*) AS N_ORDERS, COUNT(DISTINCT CUSTOMER_ID) AS CUSTOMERS,
       SUM(AMOUNT) AS TOTAL
FROM ORDERS;
Result1 row · 3 columns
N_ORDERSCUSTOMERSTOTAL
418205427.00

41 rows spread over 8 customers. Keep the 41 in mind. Every count on this page is a slice of it, and the fact that it is an odd number is going to matter shortly.

Two names, one clause

SAMPLE and TABLESAMPLE are the same clause under two names in Snowflake, so every statement on this page works with either keyword. This post uses SAMPLE throughout.

Let's run the same SAMPLE twice

The simplest form is the keyword and a number in brackets. The number is a percentage, so SAMPLE (50) asks for about half the table.

Let's run it.

Query — first run
SELECT ORDER_ID, CUSTOMER_ID, ORDER_DATE, AMOUNT
FROM ORDERS SAMPLE (50)
ORDER BY ORDER_ID;
Result23 rows · 4 columns
One draw. Captured from a single execution.
ORDER_IDCUSTOMER_IDORDER_DATEAMOUNT
10011012023-01-124200.00
10021022023-01-28890.50
10031012023-02-146150.00
10041032023-02-271500.00
10061012023-03-225020.00
10081022023-04-191120.00
10091032023-05-023340.25
10101062023-05-21640.00
10111012023-06-088100.00
10121042023-06-251975.50
10131052023-07-116420.00
10141022023-07-29750.00
10161032023-08-302860.00
10171012023-09-165540.00
10201072023-11-0710400.00
10221042023-12-092250.00
10231012023-12-276730.00
10241032024-01-153910.00
10281022024-03-211240.00
10291012024-04-067460.00
10311032024-05-104025.00
10351012024-07-188820.00
10381092024-09-115930.00

23 rows came back. Now let's run the same statement text again. Not a variation of it. The same text.

Query — same text, second run
SELECT ORDER_ID, CUSTOMER_ID, ORDER_DATE, AMOUNT
FROM ORDERS SAMPLE (50)
ORDER BY ORDER_ID;
Result27 rows · 4 columns
A second draw of the same statement. Neither grid is the one you will get.
ORDER_IDCUSTOMER_IDORDER_DATEAMOUNT
10011012023-01-124200.00
10031012023-02-146150.00
10041032023-02-271500.00
10051042023-03-092310.75
10071052023-04-057890.00
10091032023-05-023340.25
10111012023-06-088100.00
10121042023-06-251975.50
10131052023-07-116420.00
10141022023-07-29750.00
10151072023-08-149250.00
10161032023-08-302860.00
10171012023-09-165540.00
10181062023-10-031430.00
10211022023-11-24980.00
10241032024-01-153910.00
10251072024-01-3111200.00
10261052024-02-178340.00
10271062024-03-041690.00
10281022024-03-211240.00
10291012024-04-067460.00
10311032024-05-104025.00
10321072024-05-2812850.00
10341092024-06-304760.00
10351012024-07-188820.00
10361042024-08-022540.00
10371072024-08-2513100.00

27 rows this time, and a different set of orders. Nothing changed between the two runs. Not the table, not the query, not the warehouse.

If you run either statement yourself you will get neither of these grids, and no bug is involved.

Read every unseeded grid this way

This is the one page in the series whose unseeded results you cannot reproduce. Every grid on this page that has no SEED is a single captured draw, shown to demonstrate variance rather than a value to check your own output against. Where a result is repeatable, the post says so explicitly.

Now let's find out why the count moves

Two runs could be luck. So let's take six independent samples in a single statement. Six subqueries, one execution, counted side by side. Nothing about the environment can differ between them.

Query — six independent 50% samples at once
SELECT (SELECT COUNT(*) FROM ORDERS SAMPLE (50)) AS RUN_1,
       (SELECT COUNT(*) FROM ORDERS SAMPLE (50)) AS RUN_2,
       (SELECT COUNT(*) FROM ORDERS SAMPLE (50)) AS RUN_3,
       (SELECT COUNT(*) FROM ORDERS SAMPLE (50)) AS RUN_4,
       (SELECT COUNT(*) FROM ORDERS SAMPLE (50)) AS RUN_5,
       (SELECT COUNT(*) FROM ORDERS SAMPLE (50)) AS RUN_6;
Result1 row · 6 columns · scroll →
Six draws from one execution. Run it again and all six numbers change.
RUN_1RUN_2RUN_3RUN_4RUN_5RUN_6
182027232318

Six different answers to the same question. The smallest came back with 18 rows and the largest with 27, so the two ends of that grid are nine rows apart.

What should we have expected? Let's work out what half the table actually is.

Query — what half the table would be
SELECT COUNT(*) AS N_ORDERS, COUNT(*) * 0.5 AS FIFTY_PERCENT_OF_THEM
FROM ORDERS;
Result1 row · 2 columns
N_ORDERSFIFTY_PERCENT_OF_THEM
4120.5

20.5, which no sample can ever return. That is the clue. Snowflake is not counting out half the table for us.

The percentage is a probability, and it is applied to each row on its own. Every row gets its own coin flip. The number that comes back is however many flips went its way.

  • A quota would give us the same count every time and only vary which rows we got.
  • A probability per row varies both the count and the rows, and on a small table it varies them a lot.

Scale is what hides this. The same clause on a billion-row table lands so close to the percentage we asked for that the wobble stops mattering. On 41 rows we cannot ignore it, which is exactly why this demo table is small.

Now let's make it repeatable with SEED

Randomness that changes under us is a problem the moment we are testing something, writing a fixture, or asking a colleague to look at the same rows we are looking at.

SEED fixes it. We add it after the percentage.

Query — with SEED (42)
SELECT ORDER_ID, CUSTOMER_ID, ORDER_DATE, AMOUNT
FROM ORDERS SAMPLE (50) SEED (42)
ORDER BY ORDER_ID;
Result20 rows · 4 columns
ORDER_IDCUSTOMER_IDORDER_DATEAMOUNT
10041032023-02-271500.00
10051042023-03-092310.75
10071052023-04-057890.00
10091032023-05-023340.25
10101062023-05-21640.00
10121042023-06-251975.50
10131052023-07-116420.00
10141022023-07-29750.00
10151072023-08-149250.00
10191052023-10-227015.00
10201072023-11-0710400.00
10211022023-11-24980.00
10221042023-12-092250.00
10231012023-12-276730.00
10241032024-01-153910.00
10261052024-02-178340.00
10331052024-06-136900.00
10341092024-06-304760.00
10351012024-07-188820.00
10381092024-09-115930.00

20 rows. Now let's run that same text a second time, the way we did with the unseeded pair.

Query — same text, run again
SELECT ORDER_ID, CUSTOMER_ID, ORDER_DATE, AMOUNT
FROM ORDERS SAMPLE (50) SEED (42)
ORDER BY ORDER_ID;
Result20 rows · 4 columns
ORDER_IDCUSTOMER_IDORDER_DATEAMOUNT
10041032023-02-271500.00
10051042023-03-092310.75
10071052023-04-057890.00
10091032023-05-023340.25
10101062023-05-21640.00
10121042023-06-251975.50
10131052023-07-116420.00
10141022023-07-29750.00
10151072023-08-149250.00
10191052023-10-227015.00
10201072023-11-0710400.00
10211022023-11-24980.00
10221042023-12-092250.00
10231012023-12-276730.00
10241032024-01-153910.00
10261052024-02-178340.00
10331052024-06-136900.00
10341092024-06-304760.00
10351012024-07-188820.00
10381092024-09-115930.00

Identical. Same count, same orders, same order. Let's push it further and run the seeded sample six times in one statement.

Query — six seeded samples at once
SELECT (SELECT COUNT(*) FROM ORDERS SAMPLE (50) SEED (42)) AS RUN_1,
       (SELECT COUNT(*) FROM ORDERS SAMPLE (50) SEED (42)) AS RUN_2,
       (SELECT COUNT(*) FROM ORDERS SAMPLE (50) SEED (42)) AS RUN_3,
       (SELECT COUNT(*) FROM ORDERS SAMPLE (50) SEED (42)) AS RUN_4,
       (SELECT COUNT(*) FROM ORDERS SAMPLE (50) SEED (42)) AS RUN_5,
       (SELECT COUNT(*) FROM ORDERS SAMPLE (50) SEED (42)) AS RUN_6;
Result1 row · 6 columns · scroll →
RUN_1RUN_2RUN_3RUN_4RUN_5RUN_6
202020202020

The same count is not the same rows

Six identical counts would also be true of six different sets of rows that happen to be the same size. So let's prove it rather than assume it.

We can subtract one seeded sample from another with MINUS. If the two draws are the same rows, nothing is left over.

Query
SELECT ORDER_ID FROM ORDERS SAMPLE (50) SEED (42)
MINUS
SELECT ORDER_ID FROM ORDERS SAMPLE (50) SEED (42);
Result0 rows · 1 column
ORDER_ID

Empty. There is no order in the first sample that is missing from the second. The two draws are the same rows, not merely the same quantity of them.

A different seed is a different sample

Let's try three seeds in one statement.

Query — three seeds, one statement
SELECT (SELECT COUNT(*) FROM ORDERS SAMPLE (50) SEED (42))  AS SEED_42,
       (SELECT COUNT(*) FROM ORDERS SAMPLE (50) SEED (7))   AS SEED_7,
       (SELECT COUNT(*) FROM ORDERS SAMPLE (50) SEED (999)) AS SEED_999;
Result1 row · 3 columns
SEED_42SEED_7SEED_999
202124

Three seeds, three different sizes. And each of those is itself repeatable.

So a seed does not make the sample “more correct”. It picks one draw out of all the possible draws and lets us come back to it.

Let's see how far apart two seeds really are. We will subtract one from the other again.

Query — in seed 42 but not in seed 7
SELECT ORDER_ID FROM ORDERS SAMPLE (50) SEED (42)
MINUS
SELECT ORDER_ID FROM ORDERS SAMPLE (50) SEED (7);
Result12 rows · 1 column
ORDER_ID
1022
1026
1009
1033
1020
1014
1034
1019
1004
1007
1012
1021

12 of the 20 orders chosen by SEED (42) are absent from SEED (7)’s sample. Different seeds are genuinely different samples. They are not two orderings of one sample.

The limit of repeatability

Same seed, same rows — as long as the table does not change. A seed is not a saved list of row identifiers. Insert, delete or reload rows and the same seeded statement can return a different sample, without warning and without any error. If you need a frozen set of rows to keep coming back to, store the sample in a table of its own.

Now let's look at BERNOULLI and SYSTEM

There are two sampling methods. They differ in what they flip a coin for.

BERNOULLI (= ROW)SYSTEM (= BLOCK)
Decides perrowmicro-partition
A row is included ifits own flip went its waythe block it lives in was chosen
Rows near each otherare chosen independentlytend to come in or go out together
Default when no method is namedyesno

BERNOULLI is the default, which is what every statement so far has been using. Let's name it explicitly and draw it four times in one execution.

Query — BERNOULLI, four draws
SELECT (SELECT COUNT(*) FROM ORDERS SAMPLE BERNOULLI (50)) AS RUN_1,
       (SELECT COUNT(*) FROM ORDERS SAMPLE BERNOULLI (50)) AS RUN_2,
       (SELECT COUNT(*) FROM ORDERS SAMPLE BERNOULLI (50)) AS RUN_3,
       (SELECT COUNT(*) FROM ORDERS SAMPLE BERNOULLI (50)) AS RUN_4;
Result1 row · 4 columns
Four unseeded draws. These are single captured values, not figures to reproduce.
RUN_1RUN_2RUN_3RUN_4
17242225

Now let's ask for SYSTEM the same way.

Query — SYSTEM, four draws
SELECT (SELECT COUNT(*) FROM ORDERS SAMPLE SYSTEM (50)) AS RUN_1,
       (SELECT COUNT(*) FROM ORDERS SAMPLE SYSTEM (50)) AS RUN_2,
       (SELECT COUNT(*) FROM ORDERS SAMPLE SYSTEM (50)) AS RUN_3,
       (SELECT COUNT(*) FROM ORDERS SAMPLE SYSTEM (50)) AS RUN_4;
Result1 row · 4 columns
Four unseeded draws of the block-based method.
RUN_1RUN_2RUN_3RUN_4
15192127
What was expected and did not happen

An honest word about the SYSTEM grid. On 41 rows SYSTEM might reasonably have been all-or-nothing: one micro-partition, so either every row or none. It was not, and this page is not going to invent a story about why, because micro-partition layout is not something you can see from SQL. What can be said is the rule. The difference between the two methods only appears at a size this demo table cannot reach. On a small table, BERNOULLI is the one whose behaviour you can actually reason about.

ROW and BLOCK are spellings

ROW is another name for BERNOULLI and BLOCK another name for SYSTEM. Same two methods, four keywords.

Let's take one draw with each spelling.

Query — one draw each
SELECT (SELECT COUNT(*) FROM ORDERS SAMPLE ROW (50))   AS ROW_METHOD,
       (SELECT COUNT(*) FROM ORDERS SAMPLE BLOCK (50)) AS BLOCK_METHOD;
Result1 row · 2 columns
Two unseeded draws, one per method.
ROW_METHODBLOCK_METHOD
2013
Do not read that gap as evidence

ROW_METHOD and BLOCK_METHOD are far apart and they prove nothing. Both draws are unseeded, so the gap between them is variance of exactly the kind the six-draw grid measured. Comparing one draw of one method against one draw of another is not a comparison of the methods.

SYSTEM takes a seed too. And REPEATABLE is the same keyword under another name. Let's put all three spellings side by side.

Query — SEED and REPEATABLE, side by side
SELECT (SELECT COUNT(*) FROM ORDERS SAMPLE SYSTEM (50) SEED (42))       AS SYSTEM_SEED_42,
       (SELECT COUNT(*) FROM ORDERS SAMPLE SYSTEM (50) REPEATABLE (42)) AS SYSTEM_REPEATABLE_42,
       (SELECT COUNT(*) FROM ORDERS SAMPLE BLOCK (50) SEED (42))        AS BLOCK_SEED_42;
Result1 row · 3 columns
SYSTEM_SEED_42SYSTEM_REPEATABLE_42BLOCK_SEED_42
202020

All three agree, and each is repeatable on its own terms.

Let's try the edges, and a fixed number of rows

The boundary cases behave the way we would hope. Let's ask for everything.

Query — SAMPLE (100)
SELECT COUNT(*) AS N FROM ORDERS SAMPLE (100);
Result1 row · 1 column
N
41

The count is 41, which is the whole table. The probability per row was 1, so every flip went its way.

Now let's ask for nothing.

Query — SAMPLE (0)
SELECT COUNT(*) AS N FROM ORDERS SAMPLE (0);
Result1 row · 1 column
N
0

The count came back zero. No rows were sampled, and it is not an error. That is worth knowing. A percentage worked out at runtime that arrives as zero gives us an empty sample rather than a complaint.

n ROWS: a count instead of a probability

There is a second form that is often what we actually wanted. We add the keyword ROWS and the number stops being a percentage.

Query — SAMPLE (5 ROWS)
SELECT ORDER_ID, CUSTOMER_ID, AMOUNT
FROM ORDERS SAMPLE (5 ROWS)
ORDER BY ORDER_ID;
Result5 rows · 3 columns
The count is fixed at five; which five is still random, so this grid is one draw.
ORDER_IDCUSTOMER_IDAMOUNT
10011014200.00
10091033340.25
1010106640.00
10301095180.00
10341094760.00

Five rows. Not about five. Five. Let's run it three times in one statement to be sure.

Query — three draws of SAMPLE (5 ROWS)
SELECT (SELECT COUNT(*) FROM ORDERS SAMPLE (5 ROWS)) AS RUN_1,
       (SELECT COUNT(*) FROM ORDERS SAMPLE (5 ROWS)) AS RUN_2,
       (SELECT COUNT(*) FROM ORDERS SAMPLE (5 ROWS)) AS RUN_3;
Result1 row · 3 columns
RUN_1RUN_2RUN_3
555

The count is fixed and the rows are still random. That is the difference between this form and the percentage. Here we name an outcome instead of a probability.

What if we ask for more rows than exist?

Query — SAMPLE (500 ROWS) on a 41-row table
SELECT COUNT(*) AS N FROM ORDERS SAMPLE (500 ROWS);
Result1 row · 1 column
N
41

We get the table. It does not fail, and it does not pad the result with anything. The fixed size is a maximum when the table is smaller than the number we asked for.

And the one combination that is refused

Let's try to make a fixed-size sample repeatable.

Query — a fixed size with a seed
SELECT COUNT(*) AS N FROM ORDERS SAMPLE (5 ROWS) SEED (42);
Query failed — Snowflake returned an error
002325 (42601): SQL compilation error: Sampling with a fixed size does not support setting a seed.

A fixed count, or repeatability. Not both. If we need exactly five rows and the same five every time, we take a seeded percentage sample and then order and limit the result, or we save the five rows we want into a table.

Is a sample a smaller copy of the table?

It is tempting to think so. Same shape, same proportions, fewer rows. Let's check.

We will put the true average order value next to the average of two different seeded samples of the same table.

Query — the truth and two samples
SELECT (SELECT AVG(AMOUNT) FROM ORDERS)                        AS TRUE_AVG,
       (SELECT AVG(AMOUNT) FROM ORDERS SAMPLE (50) SEED (42))  AS SAMPLED_AVG_SEED_42,
       (SELECT AVG(AMOUNT) FROM ORDERS SAMPLE (50) SEED (7))   AS SAMPLED_AVG_SEED_7,
       (SELECT COUNT(*)    FROM ORDERS SAMPLE (50) SEED (42))  AS N_SEED_42,
       (SELECT COUNT(*)    FROM ORDERS SAMPLE (50) SEED (7))   AS N_SEED_7;
Result1 row · 5 columns
TRUE_AVGSAMPLED_AVG_SEED_42SAMPLED_AVG_SEED_7N_SEED_42N_SEED_7
5135.675000005005.575000005266.226190482021

Both sampled averages are in the right neighbourhood and both are wrong. SAMPLED_AVG_SEED_42 sits below TRUE_AVG and SAMPLED_AVG_SEED_7 sits above it.

The grid also carries the size of each sample, which is the only clue we would have had. And it tells us nothing about which direction the error went.

In real work we would not have TRUE_AVG at all. The reason we are sampling is that we do not want to read the whole table. So we would see one of the sampled numbers on its own, with no way to know how far off it was or which way.

Now let's sample and then GROUP BY

That was the general warning. Here is the specific one, and it is the single most useful thing on this page.

Let's take the seeded 50% sample and group it by customer.

Query — the sample, grouped by customer
SELECT CUSTOMER_ID, COUNT(*) AS N_SAMPLED
FROM ORDERS SAMPLE (50) SEED (42)
GROUP BY CUSTOMER_ID
ORDER BY CUSTOMER_ID;
Result8 rows · 2 columns
CUSTOMER_IDN_SAMPLED
1012
1022
1033
1043
1055
1061
1072
1092

A perfectly reasonable-looking grid. Now let's get the truth, from the same table with no sampling at all.

Query — the real counts
SELECT CUSTOMER_ID, COUNT(*) AS N_TRUE
FROM ORDERS
GROUP BY CUSTOMER_ID
ORDER BY CUSTOMER_ID;
Result8 rows · 2 columns
CUSTOMER_IDN_TRUE
1019
1026
1035
1044
1055
1064
1075
1093

Let's read the sampled counts and the true counts together, customer by customer. The “50%” falls apart per group.

  • Customer 105 has five orders and all five are in the sample. Five coin flips in a row went its way.
  • Customer 101 has nine orders and two are in the sample. Nowhere near half.

Neither of those is a bug, and neither is a rare accident. A 50% sample means each row had an even chance. It promises nothing at all about any group of rows. The smaller the group, the wilder its share can be.

The rule to take to work

Never sample and then GROUP BY, and never sample and then take a ratio. The per-group numbers will be nonsense, and — this is the dangerous part — they will look completely reasonable. Nothing in the output marks the groups the sample treated unfairly. Aggregate the full table and sample the aggregate if you must sample at all.

Where do we write SAMPLE, and where does SEED stop working?

SAMPLE is written against one thing in the FROM clause, and it samples that thing. So attaching it to a table inside a subquery and then joining the result is one query. Attaching it to the result of a join is a different one.

Let's sample the table first and join afterwards.

Query — sample the table, then join
SELECT COUNT(*) AS N
FROM (SELECT * FROM ORDERS SAMPLE (50) SEED (42)) O
JOIN CUSTOMERS C ON C.CUSTOMER_ID = O.CUSTOMER_ID;
Result1 row · 1 column
N
20

The count is 20, which is the size of the seeded sample of ORDERS. The join matched one customer for each sampled order and did not change the count. This form is repeatable, because the thing being sampled is a table.

Now let's do it the other way round and sample the joined result.

Query — join first, then sample the result
WITH J AS (
  SELECT O.ORDER_ID FROM ORDERS O
  JOIN CUSTOMERS C ON C.CUSTOMER_ID = O.CUSTOMER_ID
)
SELECT (SELECT COUNT(*) FROM J SAMPLE (50)) AS RUN_1,
       (SELECT COUNT(*) FROM J SAMPLE (50)) AS RUN_2,
       (SELECT COUNT(*) FROM J SAMPLE (50)) AS RUN_3,
       (SELECT COUNT(*) FROM J SAMPLE (50)) AS RUN_4,
       (SELECT COUNT(*) FROM J)             AS JOINED_ROWS;
Result1 row · 5 columns
Four unseeded draws. JOINED_ROWS is the size of the joined result they were drawn from.
RUN_1RUN_2RUN_3RUN_4JOINED_ROWS
2519262041

Four draws, four counts. JOINED_ROWS tells us the join produced 41 rows, the same as the table, so nothing was multiplied.

Why unseeded? Because let's see what happens when we add the seed.

Query — a seed on the joined result
SELECT COUNT(*) AS N
FROM (SELECT O.ORDER_ID FROM ORDERS O
      JOIN CUSTOMERS C ON C.CUSTOMER_ID = O.CUSTOMER_ID) J
SAMPLE (50) SEED (42);
Query failed — Snowflake returned an error
002089 (42601): SQL compilation error:
SAMPLE clause on views only supports row wise sampling without seed.

A join result is a view as far as this clause is concerned, and a view supports only row-wise sampling without a seed.

Let's move the SAMPLE onto the table inside the subquery and try the seed again.

Query — the seed on the table, three runs
SELECT (SELECT COUNT(*) FROM (SELECT * FROM ORDERS SAMPLE (50) SEED (42))) AS RUN_1,
       (SELECT COUNT(*) FROM (SELECT * FROM ORDERS SAMPLE (50) SEED (42))) AS RUN_2,
       (SELECT COUNT(*) FROM (SELECT * FROM ORDERS SAMPLE (50) SEED (42))) AS RUN_3;
Result1 row · 3 columns
RUN_1RUN_2RUN_3
202020

Three runs, one answer. Seeds work on tables, not on derived results. So if we need a repeatable slice of a join, we sample the tables going in rather than the rows coming out.

A second reason the position matters

That rule also tells you where to put the clause for correctness rather than repeatability. Sampling a table before a join and sampling after it are different questions: a sampled table will fail to match rows its unsampled partner still has, so a join of two independent samples keeps far less than either percentage would suggest.

Key points

  • SAMPLE and TABLESAMPLE are the same clause. SAMPLE (50) is a percentage, and the same statement text returns a different number of rows each time you run it.
  • A percentage is a probability applied per row, not a quota. On this 41-row table six independent 50% draws ranged from 18 rows to 27.
  • SEED (n) makes one draw repeatable — same count, same rows — but only while the table itself is unchanged.
  • BERNOULLI (also spelled ROW) decides per row and is the default. SYSTEM (also spelled BLOCK) decides per micro-partition. The difference between them only shows at a size a demo table cannot reach.
  • SAMPLE (n ROWS) returns exactly n rows, chosen at random; asking for more rows than the table holds returns the table, with no error and no padding.
  • A fixed size cannot take a seed, and a view or join result cannot take one either. Seeds work on tables.
  • A sample is not a smaller copy of your table. Its average is close and wrong, and nothing in the result tells you which way.
  • Never sample and then GROUP BY. In the seeded 50% sample here one customer kept all five of their orders while another kept two of nine — and the grid looks perfectly sensible.
  • No claim on this page is about speed. Nothing in this series is timed.
Next in this series

Episode 62 — MATCH_RECOGNIZE. Regular expressions over rows instead of characters: naming the shape you are looking for and letting Snowflake find every stretch of rows that has it.

Every query and every result grid on this page was captured from a live Snowflake account (version 10.34.101) running the demo script for this episode, including both error panes. The grids produced without a SEED are single draws of a random process and are not reproducible by design — they are shown to demonstrate that variance, not as values to check your own output against. The ORDERS and CUSTOMERS data is invented demo data created by that script. No performance or benchmark claims are made anywhere in this series. Clause behaviour follows the Snowflake SAMPLE / TABLESAMPLE documentation.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.