Skip to content
Storage

Primary Keys & Incremental Loading

How Keboola loads data into tables — primary keys and deduplication, incremental loads, the _timestamp column with native data types, and automatic and manual incremental processing.

How data lands in a table depends on two settings: whether the table has a primary key, and whether the load is incremental. This page explains both, plus incremental processing — letting components read only the rows that changed since their last run.

Each table may have a primary key defined on one or more columns. A primary key represents an identifier of each row in the table. Each primary key can be defined manually on a table or as part of output mapping of transformations and applications. The settings on both places must match, otherwise you will receive an error:

Output mapping does not match destination table: primary key '' does not match 'Id' in 'out.c-tutorial.opportunity_denorm' (check transformations Denormalize opportunities (id opportunity.denormalize-opportunities)).

This means that you cannot change the primary key of a table arbitrarily. Also note that you cannot set the primary key on a column which contains duplicates --- you will receive the following error:

Cannot create new primary key, duplicate values in primary key columns

If you want to manually set a primary key on a table, you can do so in Storage:

Screenshot - Create Primary Key

Then select the columns you wish to add to the primary key:

Screenshot - Select columns

To remove an existing primary key, click the bin icon:

Screenshot - Remove Primary Key

When a primary key is defined on a column, the value of that column is guaranteed to be unique in that table. As data is loaded into the table, only one of the rows with duplicate values is preserved. All the other duplicates are ignored. Let’s say you have a table with two columns: name and money. The primary key is defined on the column name.

namemoney
John$150
John$340
Darla$600
Annie$500
John$340000
Darla$600000

Their uniqueness is checked and the data are de-duplicated. The result table looks like this:

namemoney
Darla$600
John$340000
Annie$500

The order of rows in the imported file is not important and is not kept. That means that from each of the duplicate rows a randomly selected one is kept and all others are discarded. In our example, the rows John,$150, John,$340 and Darla,$600000 were discarded.

With a primary key defined on multiple columns, the combination of their values is unique. Let’s say you have a table with three columns: name, age and money. The primary key is defined on two of them: name and age. When you load the following data into your table:

nameagemoney
John15$150
John34$340
Darla60$600
Annie30$500
John34$340000
Darla60$600000

their uniqueness is checked and the data are de-duplicated. The result table looks like this:

nameagemoney
John15$150
Darla60$600
John34$340000
Annie30$500

Again, the order of rows in the imported file is not important and is not kept. In our example, the rows John,34,$340 and Darla,60,$600000 were discarded.

When a primary key is defined on a column, it is also possible to take advantage of incremental loads. If you load data into a table incrementally, new rows will be added and existing rows will be updated unless they are completely identical to the existing rows. No rows will be deleted. If you have a table with a primary key defined on the column name:

namemoney
John$150
Peter$340
Darla$600

and you import the following data to the table:

namemoney
Annie$500000
Peter$340000
Darla$600000

the result table will contain:

namemoney
John$150
Darla$600000
Peter$340000
Annie$500000

When importing data into a table with a primary key, the uniqueness is checked. The record Peter,$340000 will overwrite the row Peter,$340, because it has the same primary key value. The above applies only when incremental load is used.

When an incremental load is not used, the contents of the target table are cleared before the load. When a primary key is not defined and an incremental load is used, it simply appends the data to the table and does not update anything.

There is significant change when loading incrementally into table with native datatypes on. If a table does not have native datatypes enabled during incremental loading, the _timestamp column is updated based on the primary key only when a value in the row changes. In tables with native datatypes, the _timestamp column is updated every time when duplicate primary keys are imported. This behavior has an impact on incremental processing. When rows with duplicate primary keys are imported into tables with native types, they are treated as new rows.

  • Keboola Storage table newly created at Tue Nov 22 2022 15:37:19 GMT+0000 (1669131439)
IDNAMESKUVALUEDATE_timestamp
1JohnCD-CZ-0192472005-12-111669131439
2JackCE-CA-2235442012-10-141669131439
3JimED-BT-1352622001-04-201669131439
4JilBA-AB-1152782014-12-141669131439
  • Incremental import A1 at Wed Nov 23 2022 16:41:20 GMT+0000 (1669221680)
IDNAMESKUVALUEDATE
new row =>5AndyAB-CF-4870812003-07-05
new row =>6BethHH-FR-1475412002-04-01
  • Result of incremental import A1
IDNAMESKUVALUEDATE_timestamp
1JohnCD-CZ-0192472005-12-111669131439
2JackCE-CA-2235442012-10-141669131439
3JimED-BT-1352622001-04-201669131439
4JilBA-AB-1152782014-12-141669131439
added row = new _timestamp =>5AndyAB-CF-4870812003-07-051669221680
added row = new _timestamp =>6BethHH-FR-1475412002-04-011669221680
  • Incremental import A2 at Wed Nov 23 2022 16:42:42 GMT+0000 (1669221762)
IDNAMESKUVALUEDATE
existing row, no new values =>5AndyAB-CF-4870812003-07-05
new row =>7EdithED-BT-1394711996-12-18
  • Result of incremental import A2

Here we can see a significant change in the incremental load. The _timestamp column is updated for row id:5. For tables without native types, the row would not have the new value of _timestamp.

IDNAMESKUVALUEDATE_timestamp
1JohnCD-CZ-0192472005-12-111669131439
2JackCE-CA-2235442012-10-141669131439
3JimED-BT-1352622001-04-201669131439
4JilBA-AB-1152782014-12-141669131439
updating row = new _timestamp =>5AndyAB-CF-4870812003-07-051669221762
6BethHH-FR-1475412002-04-011669221680
added row = new _timestamp =>7EdithED-BT-1394711996-12-181669221762
  • Import A3 at Wed Nov 23 2022 16:44:34 GMT+0000 (1669221874)
IDNAMESKUVALUEDATE
existing row, with new value =>5AndyAB-CF-4860812003-07-05
existing row, no new values =>7EdithED-BT-1394711996-12-18
new row =>8KateCD-CZ-0152822008-06-07
new row =>9JoshBA-AB-1166242004-10-04
new row =>10ArthurEE-FF-665962021-04-06
  • Result of incremental import A3

  • Here we can see another change that occurs only for tables with native types. The _timestamp column for row id:7 is updated but there was no change in it.

IDNAMESKUVALUEDATE_timestamp
1JohnCD-CZ-0192472005-12-111669131439
2JackCE-CA-2235442012-10-141669131439
3JimED-BT-1352622001-04-201669131439
4JilBA-AB-1152782014-12-141669131439
updating row = new _timestamp =>5AndyAB-CF-4860812003-07-051669221874
6BethHH-FR-1475412002-04-011669221680
updating row = new _timestamp =>7EdithED-BT-1394711996-12-181669221874
added row = new _timestamp =>8KateCD-CZ-0152822008-06-071669221874
added row = new _timestamp =>9JoshBA-AB-1166242004-10-041669221874
added row = new _timestamp =>10ArthurEE-FF-665962021-04-061669221874

When a table is loaded incrementally, the update time of each row is recorded internally. This information can be later used in input mapping of many components (especially transformations). Incremental processing is available in two flavors --- automatic and manual. Incremental processing makes sense only for components reading data from the Storage (e.g., transformations and writers). Note that this is not supported for all components yet.

With automatic incremental processing, the component will receive only data modified from the last successful run of that component. Extending the above example --- if you have a table with a primary key defined on the column name:

namemoney
John$150
Peter$340
Darla$600

and you import the following data to the table:

namemoney
Darla$600000
Peter$340
Annie$500000
Melanie$900000

the result table will contain:

namemoney
John$150
Darla$600000
Peter$340
Annie$500000
Melanie$900000

Notice that the record for Peter was not updated because it was not changed at all (the imported row was completely identical to the existing one). Therefore, when using incremental processing, that row will not be loaded in input mapping.

The component (e.g., writer) will receive only the highlighted rows. If there are no added or updated rows since the last successful run, an empty table will be passed to the component. The image below shows the setting of automatic incremental processing for the Snowflake writer:

Screenshot - Automatic Incremental Processing

Screenshot - Automatic Incremental Processing Detail

Automatic incremental processing offers the most efficient way to process data, but it is less transparent compared to processing the full tables. The date used to identify newly arrived data is stored internally and can be reset via the Reset button. This will cause the component to process the entire input table on the next run.

You can always verify the load date used in a particular job using the Job events. Search for an event Exported table X:

Screenshot - Automatic Incremental Processing Events

Click the event to show its detail; the changedSince parameter shows the date used to select the added and updated data:

Screenshot - Automatic Incremental Processing Event Detail

When the Automatic Incremental Processing feature is not available, or you need greater control of the processing, you may specify the Changed in last option. This enables the component to process only a specified increment of the data:

Screenshot - Incremental Processing

Extending the above example --- if you have a table with a primary key defined on the column name:

namemoney
John$150
Peter$340
Darla$600

and you import the following data to the table:

namemoney
Darla$600000
Peter$340
Annie$500000
Melanie$900000

assuming that the import was on 2010-01-02 10:00, the result table will contain (the *_timestamp* column is not an actual column of the table, it is just displayed here for illustration purposes):

namemoney*_timestamp*
John$1502010-01-01 10:00
Darla$6000002010-01-02 10:00
Peter$3402010-01-01 10:00
Annie$5000002010-01-02 10:00
Melanie$9000002010-01-02 10:00

Therefore, three rows in the table will be considered as changed (either added or updated). Now when you run a component (e.g., transformation) with the Changed in last option, various things can happen:

  • Changed in last is set to 1 day, and the component is started any time between 2010-01-02 10:00 and 2010-01-03 10:00three rows will be exported from the table.
  • Changed in last is set to 1 day, and the component is started any time after 2010-01-03 10:00no rows will be exported from the table.
  • Changed in last is set to 2 day, and the component is started any time between 2010-01-02 10:00 and 2010-01-03 10:00five rows will be exported from the table.

Notice that the record for Peter was not updated because it did not change (the imported row was completely identical to the existing one). Therefore, when using incremental processing set to 1 day, that row will not be included in the input mapping.

Ask Kai

Ask anything about Keboola — I'll search the docs and cite the pages I use.