Subset a Database
Copy a fraction of a database’s rows into a smaller test database. Every row you keep still has the rows it refers to, so foreign keys stay valid and joins still work.
Before you start
-
An input and an output data source in the same project, with the output set to Read & write.
-
An output database you can overwrite. See Database permissions.
Subsetting isn’t a separate workflow type. You set target_ratio, the fraction of rows to keep, in a workflow’s configuration, in KEEP or MASKING mode, so you can mask in the same run.
Subset the database
To keep about half of every table and copy the values unchanged:
default_config:
mode: KEEP
target_ratio: 0.5
table_truncation_mode: TRUNCATE
schema_creation_mode: CREATE_IF_NOT_EXISTS
-
Web UI
-
CLI
-
Open the project and click Create new workflow.
-
In the Define step, name the workflow, choose Create empty workflow, and click Next.
-
In the Connect step, choose the input and output data sources, then click Create workflow.
-
In the Configuration tab, replace the configuration with the YAML above and click Save.
-
Click Run. When the run is done, its status in Run history is
COMPLETED.
Save the YAML above as config.yaml and run it with the TDK CLI:
tdk \
--input-url <input JDBC URL> --input-username <user> --input-password <password> \
--output-url <output JDBC URL> --output-username <user> --output-password <password> \
--config-file config.yaml
The masking guide shows how to start the sample databases used in these examples.
To mask the data you keep, use mode: MASKING instead of KEEP.
Check the result
Count the rows in each table of the output database, and compare with the input:
select 'actor' as "table", count(*) as numrows from actor
union select 'film', count(*) from film
union select 'film_actor', count(*) from film_actor
union select 'rental', count(*) from rental
order by 1;
With target_ratio: 0.5 on the sample database, actor is halved, but some tables shrink much more:
| Table | Input rows | Output rows | Why |
|---|---|---|---|
|
200 |
100 |
Halved |
|
1000 |
0 |
A film row needs the |
|
5462 |
0 |
Refers to |
|
16044 |
0 |
Refers to |
Subset only the largest tables
To keep the small reference tables whole and cut only the big ones, set target_ratio per table. Settings in tables override default_config:
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
table_truncation_mode: TRUNCATE
schema_creation_mode: CREATE_IF_NOT_EXISTS
Now every film is kept, and only rental and payment shrink, to about a tenth:
| Table | Input rows | Output rows |
|---|---|---|
|
1000 |
1000 |
|
5462 |
5462 |
|
16049 |
1605 |
|
16044 |
1605 |
To keep rows by date, ID range or another condition rather than by fraction, see Filter source rows.
If it fails
| Problem | What to do |
|---|---|
A table you need is empty or much smaller than expected |
Check what it refers to. Keep its parent tables whole by setting |
The run stops on a circular reference between tables |
Set a cycle resolution strategy. |
A relationship isn’t kept because the database has no foreign key for it |
Add a virtual foreign key. |
The run fails with duplicate key errors in the output |
The output still holds rows from an earlier run. Set |