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
- Let's run the same SAMPLE twice
- Now let's find out why the count moves
- Now let's make it repeatable with SEED
- Now let's look at BERNOULLI and SYSTEM
- Let's try the edges, and a fixed number of rows
- Is a sample a smaller copy of the table?
- Now let's sample and then GROUP BY
- Where do we write SAMPLE, and where does SEED stop working?
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.
-- 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.
SELECT COUNT(*) AS N_ORDERS, COUNT(DISTINCT CUSTOMER_ID) AS CUSTOMERS,
SUM(AMOUNT) AS TOTAL
FROM ORDERS;| N_ORDERS | CUSTOMERS | TOTAL |
|---|---|---|
| 41 | 8 | 205427.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.
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.
SELECT ORDER_ID, CUSTOMER_ID, ORDER_DATE, AMOUNT
FROM ORDERS SAMPLE (50)
ORDER BY ORDER_ID;| ORDER_ID | CUSTOMER_ID | ORDER_DATE | AMOUNT |
|---|---|---|---|
| 1001 | 101 | 2023-01-12 | 4200.00 |
| 1002 | 102 | 2023-01-28 | 890.50 |
| 1003 | 101 | 2023-02-14 | 6150.00 |
| 1004 | 103 | 2023-02-27 | 1500.00 |
| 1006 | 101 | 2023-03-22 | 5020.00 |
| 1008 | 102 | 2023-04-19 | 1120.00 |
| 1009 | 103 | 2023-05-02 | 3340.25 |
| 1010 | 106 | 2023-05-21 | 640.00 |
| 1011 | 101 | 2023-06-08 | 8100.00 |
| 1012 | 104 | 2023-06-25 | 1975.50 |
| 1013 | 105 | 2023-07-11 | 6420.00 |
| 1014 | 102 | 2023-07-29 | 750.00 |
| 1016 | 103 | 2023-08-30 | 2860.00 |
| 1017 | 101 | 2023-09-16 | 5540.00 |
| 1020 | 107 | 2023-11-07 | 10400.00 |
| 1022 | 104 | 2023-12-09 | 2250.00 |
| 1023 | 101 | 2023-12-27 | 6730.00 |
| 1024 | 103 | 2024-01-15 | 3910.00 |
| 1028 | 102 | 2024-03-21 | 1240.00 |
| 1029 | 101 | 2024-04-06 | 7460.00 |
| 1031 | 103 | 2024-05-10 | 4025.00 |
| 1035 | 101 | 2024-07-18 | 8820.00 |
| 1038 | 109 | 2024-09-11 | 5930.00 |
23 rows came back. Now let's run the same statement text again. Not a variation of it. The same text.
SELECT ORDER_ID, CUSTOMER_ID, ORDER_DATE, AMOUNT
FROM ORDERS SAMPLE (50)
ORDER BY ORDER_ID;| ORDER_ID | CUSTOMER_ID | ORDER_DATE | AMOUNT |
|---|---|---|---|
| 1001 | 101 | 2023-01-12 | 4200.00 |
| 1003 | 101 | 2023-02-14 | 6150.00 |
| 1004 | 103 | 2023-02-27 | 1500.00 |
| 1005 | 104 | 2023-03-09 | 2310.75 |
| 1007 | 105 | 2023-04-05 | 7890.00 |
| 1009 | 103 | 2023-05-02 | 3340.25 |
| 1011 | 101 | 2023-06-08 | 8100.00 |
| 1012 | 104 | 2023-06-25 | 1975.50 |
| 1013 | 105 | 2023-07-11 | 6420.00 |
| 1014 | 102 | 2023-07-29 | 750.00 |
| 1015 | 107 | 2023-08-14 | 9250.00 |
| 1016 | 103 | 2023-08-30 | 2860.00 |
| 1017 | 101 | 2023-09-16 | 5540.00 |
| 1018 | 106 | 2023-10-03 | 1430.00 |
| 1021 | 102 | 2023-11-24 | 980.00 |
| 1024 | 103 | 2024-01-15 | 3910.00 |
| 1025 | 107 | 2024-01-31 | 11200.00 |
| 1026 | 105 | 2024-02-17 | 8340.00 |
| 1027 | 106 | 2024-03-04 | 1690.00 |
| 1028 | 102 | 2024-03-21 | 1240.00 |
| 1029 | 101 | 2024-04-06 | 7460.00 |
| 1031 | 103 | 2024-05-10 | 4025.00 |
| 1032 | 107 | 2024-05-28 | 12850.00 |
| 1034 | 109 | 2024-06-30 | 4760.00 |
| 1035 | 101 | 2024-07-18 | 8820.00 |
| 1036 | 104 | 2024-08-02 | 2540.00 |
| 1037 | 107 | 2024-08-25 | 13100.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.
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.
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;| RUN_1 | RUN_2 | RUN_3 | RUN_4 | RUN_5 | RUN_6 |
|---|---|---|---|---|---|
| 18 | 20 | 27 | 23 | 23 | 18 |
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.
SELECT COUNT(*) AS N_ORDERS, COUNT(*) * 0.5 AS FIFTY_PERCENT_OF_THEM
FROM ORDERS;| N_ORDERS | FIFTY_PERCENT_OF_THEM |
|---|---|
| 41 | 20.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.
SELECT ORDER_ID, CUSTOMER_ID, ORDER_DATE, AMOUNT
FROM ORDERS SAMPLE (50) SEED (42)
ORDER BY ORDER_ID;| ORDER_ID | CUSTOMER_ID | ORDER_DATE | AMOUNT |
|---|---|---|---|
| 1004 | 103 | 2023-02-27 | 1500.00 |
| 1005 | 104 | 2023-03-09 | 2310.75 |
| 1007 | 105 | 2023-04-05 | 7890.00 |
| 1009 | 103 | 2023-05-02 | 3340.25 |
| 1010 | 106 | 2023-05-21 | 640.00 |
| 1012 | 104 | 2023-06-25 | 1975.50 |
| 1013 | 105 | 2023-07-11 | 6420.00 |
| 1014 | 102 | 2023-07-29 | 750.00 |
| 1015 | 107 | 2023-08-14 | 9250.00 |
| 1019 | 105 | 2023-10-22 | 7015.00 |
| 1020 | 107 | 2023-11-07 | 10400.00 |
| 1021 | 102 | 2023-11-24 | 980.00 |
| 1022 | 104 | 2023-12-09 | 2250.00 |
| 1023 | 101 | 2023-12-27 | 6730.00 |
| 1024 | 103 | 2024-01-15 | 3910.00 |
| 1026 | 105 | 2024-02-17 | 8340.00 |
| 1033 | 105 | 2024-06-13 | 6900.00 |
| 1034 | 109 | 2024-06-30 | 4760.00 |
| 1035 | 101 | 2024-07-18 | 8820.00 |
| 1038 | 109 | 2024-09-11 | 5930.00 |
20 rows. Now let's run that same text a second time, the way we did with the unseeded pair.
SELECT ORDER_ID, CUSTOMER_ID, ORDER_DATE, AMOUNT
FROM ORDERS SAMPLE (50) SEED (42)
ORDER BY ORDER_ID;| ORDER_ID | CUSTOMER_ID | ORDER_DATE | AMOUNT |
|---|---|---|---|
| 1004 | 103 | 2023-02-27 | 1500.00 |
| 1005 | 104 | 2023-03-09 | 2310.75 |
| 1007 | 105 | 2023-04-05 | 7890.00 |
| 1009 | 103 | 2023-05-02 | 3340.25 |
| 1010 | 106 | 2023-05-21 | 640.00 |
| 1012 | 104 | 2023-06-25 | 1975.50 |
| 1013 | 105 | 2023-07-11 | 6420.00 |
| 1014 | 102 | 2023-07-29 | 750.00 |
| 1015 | 107 | 2023-08-14 | 9250.00 |
| 1019 | 105 | 2023-10-22 | 7015.00 |
| 1020 | 107 | 2023-11-07 | 10400.00 |
| 1021 | 102 | 2023-11-24 | 980.00 |
| 1022 | 104 | 2023-12-09 | 2250.00 |
| 1023 | 101 | 2023-12-27 | 6730.00 |
| 1024 | 103 | 2024-01-15 | 3910.00 |
| 1026 | 105 | 2024-02-17 | 8340.00 |
| 1033 | 105 | 2024-06-13 | 6900.00 |
| 1034 | 109 | 2024-06-30 | 4760.00 |
| 1035 | 101 | 2024-07-18 | 8820.00 |
| 1038 | 109 | 2024-09-11 | 5930.00 |
Identical. Same count, same orders, same order. Let's push it further and run the seeded sample six times in one statement.
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;| RUN_1 | RUN_2 | RUN_3 | RUN_4 | RUN_5 | RUN_6 |
|---|---|---|---|---|---|
| 20 | 20 | 20 | 20 | 20 | 20 |
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.
SELECT ORDER_ID FROM ORDERS SAMPLE (50) SEED (42)
MINUS
SELECT ORDER_ID FROM ORDERS SAMPLE (50) SEED (42);| 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.
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;| SEED_42 | SEED_7 | SEED_999 |
|---|---|---|
| 20 | 21 | 24 |
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.
SELECT ORDER_ID FROM ORDERS SAMPLE (50) SEED (42)
MINUS
SELECT ORDER_ID FROM ORDERS SAMPLE (50) SEED (7);| 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.
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 per | row | micro-partition |
| A row is included if | its own flip went its way | the block it lives in was chosen |
| Rows near each other | are chosen independently | tend to come in or go out together |
| Default when no method is named | yes | no |
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.
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;| RUN_1 | RUN_2 | RUN_3 | RUN_4 |
|---|---|---|---|
| 17 | 24 | 22 | 25 |
Now let's ask for SYSTEM the same way.
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;| RUN_1 | RUN_2 | RUN_3 | RUN_4 |
|---|---|---|---|
| 15 | 19 | 21 | 27 |
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.
SELECT (SELECT COUNT(*) FROM ORDERS SAMPLE ROW (50)) AS ROW_METHOD,
(SELECT COUNT(*) FROM ORDERS SAMPLE BLOCK (50)) AS BLOCK_METHOD;| ROW_METHOD | BLOCK_METHOD |
|---|---|
| 20 | 13 |
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.
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;| SYSTEM_SEED_42 | SYSTEM_REPEATABLE_42 | BLOCK_SEED_42 |
|---|---|---|
| 20 | 20 | 20 |
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.
SELECT COUNT(*) AS N FROM ORDERS SAMPLE (100);| 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.
SELECT COUNT(*) AS N FROM ORDERS SAMPLE (0);| 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.
SELECT ORDER_ID, CUSTOMER_ID, AMOUNT
FROM ORDERS SAMPLE (5 ROWS)
ORDER BY ORDER_ID;| ORDER_ID | CUSTOMER_ID | AMOUNT |
|---|---|---|
| 1001 | 101 | 4200.00 |
| 1009 | 103 | 3340.25 |
| 1010 | 106 | 640.00 |
| 1030 | 109 | 5180.00 |
| 1034 | 109 | 4760.00 |
Five rows. Not about five. Five. Let's run it three times in one statement to be sure.
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;| RUN_1 | RUN_2 | RUN_3 |
|---|---|---|
| 5 | 5 | 5 |
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?
SELECT COUNT(*) AS N FROM ORDERS SAMPLE (500 ROWS);| 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.
SELECT COUNT(*) AS N FROM ORDERS SAMPLE (5 ROWS) SEED (42);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.
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;| TRUE_AVG | SAMPLED_AVG_SEED_42 | SAMPLED_AVG_SEED_7 | N_SEED_42 | N_SEED_7 |
|---|---|---|---|---|
| 5135.67500000 | 5005.57500000 | 5266.22619048 | 20 | 21 |
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.
SELECT CUSTOMER_ID, COUNT(*) AS N_SAMPLED
FROM ORDERS SAMPLE (50) SEED (42)
GROUP BY CUSTOMER_ID
ORDER BY CUSTOMER_ID;| CUSTOMER_ID | N_SAMPLED |
|---|---|
| 101 | 2 |
| 102 | 2 |
| 103 | 3 |
| 104 | 3 |
| 105 | 5 |
| 106 | 1 |
| 107 | 2 |
| 109 | 2 |
A perfectly reasonable-looking grid. Now let's get the truth, from the same table with no sampling at all.
SELECT CUSTOMER_ID, COUNT(*) AS N_TRUE
FROM ORDERS
GROUP BY CUSTOMER_ID
ORDER BY CUSTOMER_ID;| CUSTOMER_ID | N_TRUE |
|---|---|
| 101 | 9 |
| 102 | 6 |
| 103 | 5 |
| 104 | 4 |
| 105 | 5 |
| 106 | 4 |
| 107 | 5 |
| 109 | 3 |
Let's read the sampled counts and the true counts together, customer by customer. The “50%” falls apart per group.
- Customer
105has five orders and all five are in the sample. Five coin flips in a row went its way. - Customer
101has 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.
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.
SELECT COUNT(*) AS N
FROM (SELECT * FROM ORDERS SAMPLE (50) SEED (42)) O
JOIN CUSTOMERS C ON C.CUSTOMER_ID = O.CUSTOMER_ID;| 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.
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;| RUN_1 | RUN_2 | RUN_3 | RUN_4 | JOINED_ROWS |
|---|---|---|---|---|
| 25 | 19 | 26 | 20 | 41 |
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.
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);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.
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;| RUN_1 | RUN_2 | RUN_3 |
|---|---|---|
| 20 | 20 | 20 |
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.
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
SAMPLEandTABLESAMPLEare 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 spelledROW) decides per row and is the default.SYSTEM(also spelledBLOCK) 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.
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.