TSCreatePatternIndex function
The TSCreatePatternIndex function indexes time series data for pattern matching searches.
Syntax
TSCreatePatternIndex(
ts TimeSeries,
table_name LVARCHAR,
primary_key LVARCHAR,
key_value LVARCHAR,
begin_tstamp DATETIME YEAR TO FRACTION(5),
end_tstamp DATETIME YEAR TO FRACTION(5),
col_name LVARCHAR,
index_settings LVARCHAR,
scope INTEGER DEFAULT 0)
returns INTEGER;
- ts
- The time series value for the specified primary key.
- table_name
- The name of the time series table. The table must contain a TimeSeries column and have a primary key.
- primary_key
- The primary key column name.
- key_value
- The value of the primary key for the time series instance to index.
- begin_stamp
- The beginning point of the range to index. Can be NULL, which represents the first element in the time series.
- end_stamp
- The ending point of the range to index. Can be NULL, which represents the last element in the time series.
- col_name
- The name of the column in the TimeSeries subtype to index.
- index_settings
- A string of index properties in one of the following
formats:
'idx_metatype=0 slidewin=value' 'idx_metatype=1 partitionlen=value'
- scope
- Whether to update an existing index or replace the existing index:
Usage
Run the TSCreatePatternIndex function to index the data in the specified time series column for the specified time range. You can customize the index settings to obtain the appropriate search results. When you run a pattern matching query for which a pattern matching index exists, the query uses the index to speed the search.
For each time series instance, as designated by a primary key value, you can create both a whole pattern match index and a subsequence pattern match index on each column in the TimeSeries subtype that contains numeric data.
If you want to extend the time range of an existing index, conform to the following rules:
- Set the scope value to 0.
- You cannot have gaps in the time range of the index. Set the begin_stamp value to before the end_stamp of the existing index. Set the end_stamp value to after the begin_stamp of the existing index.
- You can extend the index time range in either direction, or both directions. The value of begin_stamp can be earlier than the begin_stamp of the existing index. The value of end_stamp can be after the end_stamp of the existing index.
When you update an index, you can set the isolation level to Last Committed.
Returns
- 0 = The index was created.
- An error.
Example: Create an index
The example uses the following TimeSeries subtype definition, table, and data:
CREATE ROW TYPE myrow(tstamp datetime year to fraction(5), value1 real);
CREATE TABLE tsdata(id int primary key, ts1 timeseries(myrow));
INSERT INTO tsdata VALUES(1000,
"origin(2011-01-01 00:00:00.00000), calendar(ts_1month),
container(container), threshold(0), regular,
[(1),(1),(55),(55),(55),(55),(1),(45),(45),(45),(45),(1)]");
The following statement creates a pattern matching index on the value1 column in the TimeSeries subtype for the time series instance that has a primary key value of 1000:
SELECT TSCreatePatternIndex(ts1, 'tsdata', 'id',
1000,'2011-01-01 00:00:00.00000','2011-12-01 00:00:00.00000',
'value1', 'idx_metatype=0 slidewin=4')
FROM tsdata WHERE id = 1000;
Example: Extend the index time range
The following statement extends the time range of the index from the previous example:
SELECT TSCreatePatternIndex(ts1, 'tsdata', 'id',
1000,'2011-12-01 00:00:00.00000','2014-12-01 00:00:00.00000',
'value1', 'idx_metatype=0 slidewin=4')
FROM tsdata WHERE id = 1000;
The index is extended to December 1, 2014.
Example: Indexing a BSON field
The example uses the following TimeSeries subtype definition, table, and time series definition:
CREATE ROW TYPE ts_int (tstamp datetime year to fraction(5), value bson);
CREATE TABLE tsdata (id int primary key, ts timeseries(ts_int));
EXECUTE PROCEDURE TSContainerCreate('container', 'rootdbs', 'ts_int', 00, 50);
INSERT INTO tsdata VALUES(3000, 'origin(2011-01-01 00:00:00.00000),
calendar(ts_1min), container(container), threshold(0), regular, []);
The BSON document in the value column contains a field that is named v1 that
contains numeric data, for example, {"v1":10}
.
The following statement creates a pattern matching index on the v1 field in the value column of the TimeSeries subtype for the time series instance that has a primary key value of 3000:
SELECT TSCreatePatternIndex(ts,'tsdata','id','3000',
'2011-01-01 00:00:00.00000','2011-11-01 02:18:00.00000',
'value.v1',
'idx_metatype=0 partitionlen=5 aggwin=1 slidewin=4 uerror=1.0',1)
FROM tsdata
WHERE id = 3000;