latestKeyedTable
Syntax
latestKeyedTable(keyColumns, timeColumn, [X1], [X2], …..)
or
latestKeyedTable(keyColumns, timeColumn, capacity:size, colNames, colTypes)
or
latestKeyedTable(keyColumns, timeColumn, table)
Arguments
Added parameter timeColumn for the function keyedTable.
timeColumn is a string indicating the column name of the time column. The time column can be integral or temporal type.
Details
Create a keyed table, which is a special type of in-memory table with primary key. The primary key can be one column or multiple columns. Compared to the keyedTable
, latestKeyedTable
adds a time column to determine whether to update records.
When a new record is appended to the keyed table, if its timestamp is smaller than that of the existing row which has the same primary key, it does not overwrite the existing row. latestKeyedTable
deduplicates records with the same primary key based on the time column, which affects its writing performance (relatively slow compared with keyedTable
).
Note:
The primary key cannot be modified (with functions
update
, orreplaceColumn!
) or deleted (with functionsalter
, ordropColumns!
).The function cannot be used to create a table containing array vectors.
Refer to keyedTable
for the optimization of query performance on latestKeyedTable.
Examples
Example 1. Create a keyed table.
Scenario 1:
$ sym=`A`B`C`D`E
$ id=5 4 3 2 1
$ val=52 64 25 48 71
$ timeCol = 2022.12.07T00:00:00.001+0..4
$ t=latestKeyedTable(`sym`id,`timeCol,sym,id,timeCol,val)
$ t;
sym | id | timeCol | val |
---|---|---|---|
A | 5 | 2022.12.07T00:00:00.001 | 52 |
B | 4 | 2022.12.07T00:00:00.002 | 64 |
C | 3 | 2022.12.07T00:00:00.003 | 25 |
D | 2 | 2022.12.07T00:00:00.004 | 48 |
E | 1 | 2022.12.07T00:00:00.005 | 71 |
Scenario 2:
$ t=latestKeyedTable(`sym`id,1:0,`sym`id`val,[SYMBOL,INT,INT])
$ insert into t values(`A`B`C`D`E,5 4 3 2 1,2022.12.07T00:00:00.001+0..4,52 64 25 48 71);
Scenario 3:
$ tmp=table(sym, id, timeCol, val)
$t=latestKeyedTable(`sym`id, `timeCol, tmp);
Example 2. Update a keyed table.
If the new row has the same primary key value as an existing row, whether to update the record is determined by the time column.
$ insert into t values(`A`A`E,5 5 1, 2022.12.07T00:00:00.001 2022.12.07T00:00:00.007 2022.12.07T00:00:00.003, 44 66 28);
$ t;
sym | id | timeCol | val |
---|---|---|---|
A | 5 | 2022.12.07T00:00:00.007 | 66 |
B | 4 | 2022.12.07T00:00:00.002 | 64 |
C | 3 | 2022.12.07T00:00:00.003 | 25 |
D | 2 | 2022.12.07T00:00:00.004 | 48 |
E | 1 | 2022.12.07T00:00:00.005 | 71 |
Related functions: keyedTable, indexedTable, latestIndexedTable