How Subsetting Works
Subsetting writes a smaller copy of the input in which every foreign key still points to an existing row. It isn’t a separate mode: you make a table smaller with target_ratio or a filter, in KEEP or MASKING mode, so you can mask and subset in the same run.
Two ways to make a table smaller
| Setting | What it does |
|---|---|
|
The share of rows to keep, between 0 and 1. Set it in |
|
An SQL condition, as in a |
Rows are chosen at random within what the ratio and the filter allow.
Parents first
Tables are processed parents first, and a row is kept only if the rows it references were kept. So a child table shrinks with its parent, even if you set no ratio on the child. There are two exceptions: a nullable foreign key to a parent table that ends up with no rows at all is set to null, and the row is kept; and references you list in cycle_breaker_references are ignored.
The same goes for a filter: filtering film to one film also limits inventory to that film’s copies, and rental to their rentals.
This rule keeps the output consistent, with no orphaned rows. It also explains the results that surprise people:
-
A table has fewer rows than its ratio. Its parents were reduced too, and the reductions add up down the chain.
-
A table is empty. None of the rows it depends on were kept. In Subset a Database,
target_ratio: 0.5on every table leavesfilmempty, and with itfilm_actorandrental, which depend on films.filmreferenceslanguage, a small table, so when the rows films reference aren’t among the kept half oflanguage, no film can be kept.
Controlling the result
-
Reduce only the large tables. Set
target_ratioon the big transaction tables, and leave small lookup tables at the default of 1:default_config: mode: KEEP tables: - table_name_with_schema: "public.rental" target_ratio: 0.1 - table_name_with_schema: "public.payment" target_ratio: 0.1 -
Start from the entities you need. Put the
filteror the ratio on a parent, such as customers or accounts; their child rows follow. -
Keep lookup tables whole. A ratio on a table that many others reference, like
languageabove, empties everything below it when the wrong rows are dropped. -
Mask at the same time. Use
mode: MASKINGwith the same settings to get a smaller, masked copy in one run.
Cycles
When foreign keys form a loop, such as an employee table that references itself for each employee’s manager, there’s no table to start from. The cycle_resolution_strategy setting decides what happens:
| Strategy | What it does |
|---|---|
|
Stops with an error, unless you name the references to break in |
|
Breaks the cycle at the last nullable reference, which is set to null in the output. |
|
Disables the foreign keys in the cycle while writing, then turns them back on. Oracle, SQL Server, MySQL and Db2 only, in |
See the Configuration Reference.
Related pages
-
Subsetting Data: step-by-step guides.
-
Workflows and Runs: where subsetting fits in a run.