This documentation is available to agents as Markdown at https://docs.synthesized.io/tdk/latest/user_guide/020_guides/subsetting_data/subsetting.md. For the complete documentation index, see llms.txt.

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

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

  1. Open the project and click Create new workflow.

  2. In the Define step, name the workflow, choose Create empty workflow, and click Next.

  3. In the Connect step, choose the input and output data sources, then click Create workflow.

  4. In the Configuration tab, replace the configuration with the YAML above and click Save.

  5. 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

actor

200

100

Halved

film

1000

0

A film row needs the language row it refers to, and language was halved too. When the rows films reference aren’t kept, no film can be kept

film_actor

5462

0

Refers to film

rental

16044

0

Refers to inventory, which refers to film

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

film

1000

1000

film_actor

5462

5462

payment

16049

1605

rental

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 target_ratio per table, as above.

The run stops on a circular reference between tables

Set a cycle resolution strategy. DELETE_NOT_REQUIRED sets the cyclic reference columns to null.

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 table_truncation_mode: TRUNCATE to empty the tables first.

Next