Pre-downsampling can be used to pre-calculate data at the specified time interval when data is written to the database. The pre-downsampled data is stored separately. During a downsampling query, the matched pre-downsampling data can be automatically queried to reduce the access latency. This topic describes the concept of pre-downsampling and how to create and manage pre-downsampling rules. This topic also provides best practices for pre-downsampling.
Prerequisites
The LindormTSDB version of the Lindorm instance is 3.4.19 or later.
For more information about how to view and upgrade the LindormTSDB version of the Lindorm instance, see Release notes of LindormTSDB and Upgrade the minor engine version of a Lindorm instance.
Overview
Pre-downsampling
Pre-downsampling is performed to calculate data before it is written to databases. Pre-downsampling downsamples and separately stores the data to be written based on the specified pre-downsampling rule. When you query the written data, data that is downsampled at a pre-downsampling ratio that is closest to the downsampling ratio specified in the query conditions is queried. Then, the queried data is sent to and processed by the compute engine. This way, the amount of data that needs to be queried and calculated in real time is reduced and so that the query latency is decreased.
Compared with continuous queries, pre-downsampling has the following benefits:
The downsampled data can be queried immediately queried after it is written. You do not need to wait for the execution of continuous queries.
Pre-downsampling does not affect the write performance. Downsampled data is stored in real time when it is written.
The pre-downsampled data and raw data is stored in the same table. When you perform pre-downsampling queries, pre-downsampled data that best matches the specified rule is queried. If the pre-downsampled data does not match the specified rule, the raw data is queried.
Pre-downsampling rules
Pre-downsampling rules define the time granularity of pre-downsampling operations and how data is aggregated in pre-downsampling. For example, if a row of raw data is generated each minutes, you can configure a pre-downsampling rule to perform pre-downsampling based on the average value of the raw data at an hourly basis. Pre-downsampling rules can be configured for databases or tables. The following figure shows the difference between the two types of pre-downsampling rules.
Create a pre-downsampling rule
A pre-downsampling rule applies only to data that is written to the table or database before the rule is created.
Create a pre-downsampling rule for a table by using SQL statements
Examples
Configure a pre-downsampling rule in which the pre-downsampling ratio is set to one hour and the sum and max operations are performed on the pre-downsampled data.
CREATE PREDOWNSAMPLE `1h` AGGREGATORS (`sum`, `max`) ON sensor;
Configure a pre-downsampling rule in which the pre-downsampling ratio is set to one hour, the sum and max operations are performed on the pre-downsampled data, and the TTL of the pre-downsampled data is set to 90 days.
CREATE PREDOWNSAMPLE `1h` AGGREGATORS (`sum`, `max`) TTL `90d` ON sensor;
Syntax reference
Create a pre-downsampling rule for a database by using API operations
Request path and method
Request path | Request method | Description |
/api/database |
| Configure a pre-downsampling rule for the specified database. If the specified database (such as the default database) already exists, use the PUT method to update the pre-downsampling rules for the database. To configure pre-downsampling rules for a database when you create the database, use the POST method. |
Request parameters
Parameter | Required | Description |
name | Yes | The database name. Example: default. |
downsampleRules | Yes | The list of pre-downsampling rules that you want to configure. Each time you update the pre-downsampling rules of the database, all rules in the list are updated. |
Parameters for a single pre-downsampling rule
Parameter | Required | Description | Example |
aggregator | Yes | The aggregate function used in pre-downsampling. Valid values: sum, count, max, min, first, last, and median. For more information about aggregate functions, see CREATE PREDOWNSAMPLE. Important
| "aggregator": "sum" |
intervals | Yes | The list of time intervals based on which pre-downsampling is performed. The following units are supported: s (second), m (minute), h (hour), and d (day). | For example, if the parameter is set to ["10m", "1h"] , the raw data is separately pre-downsampled based on an interval of 10 minutes and an interval of one hour. |
Sample request
PUT /api/database
{
"name": "default",
"downsampleRules": {
"rules": [
{
"aggregator": "sum",
"intervals": [
"1m",
"1h"
]
},
{
"aggregator": "max",
"intervals": [
"1m",
"1h"
]
}
]
}
}
Response description
If the request is successful, the HTTP status code 200 is returned.
Query pre-downsampling rules
The method that you can use to query pre-downsampling rules depends on whether the rules are configured for databases or tables. You can use API operations query pre-downsampling rules configured for databases and use SQL statements to query pre-downsampling rules configured for tables.
Query pre-downsampling rules configured for a table by using SQL statements
Example
Query all pre-downsampling rules configured for all tables in a database.
SHOW PREDOWNSAMPLES;
Query all pre-downsampling rules configured for a table named sensor.
SHOW PREDOWNSAMPLES ON sensor;
Syntax reference
Query pre-downsampling rules configured for a database by using API operations
Request path and method
Request path | Request method | Description |
/api/database | GET | Query the pre-downsampling rules configured for a database. |
Sample response
[{
"name": "default",
"downsampleRules": {
"rules": [
{
"aggregator": "sum",
"intervals": [
"1m",
"1h"
]
},
{
"aggregator": "max",
"intervals": [
"1m",
"1h"
]
}
]
}
}]
Response description
If the request is successful, the HTTP status code 204 is returned.
Delete a pre-downsampling rule
The method that you can use to delete pre-downsampling rules depends on whether the rules are configured for databases or tables. You can use API operations delete pre-downsampling rules configured for databases and use SQL statements to delete pre-downsampling rules configured for tables.
After you delete a pre-downsampling rule, data that has been pre-downsampled based on the rule is not immediately deleted. However, the data is not returned when you query pre-downsampled data. Pre-downsampled data is automatically deleted after it expires.
Delete pre-downsampling rules configured for a table by using SQL statements
Example
DROP PREDOWNSAMPLE `1h` AGGREGATORS (`sum`, `max`) ON sensor;
Syntax reference
Delete pre-downsampling rules configured for a database by using API operations
The request path, request method, and request parameters that you need to configure to delete pre-downsampling rules for a database are the same as those when you create a pre-downsampling rule for a database. However, you need to set the downsampleRules field to empty. The HTTP status code that is returned in the response is also the same as that when you create a pre-downsampling rule for a database. For more information, see Create a pre-downsampling rule for a database by using API operations.
Sample request
PUT /api/database
{
"name": "default",
"downsampleRules": {
}
}
Query pre-downsampled data
Matching rules for pre-downsampled data
If pre-downsampling rules are configured for a database and a table in the database at the same time, the rules configured for the table prevail in pre-downsampling queries.
If the downsampling interval specified in a query does not exactly match that of the pre-downsampled data, the pre-downsampled data whose pre-downsampling interval best matches the specified downsampling interval is queried. We recommend that you set the time interval based on which pre-downsampling is performed to 1m, 10m, 1h, or 1d to meet the requirements of most downsampling queries.
NoteIf the pre-downsampling interval of the pre-downsampled data is the maximum value that can divide the downsampling interval specified in a query, the pre-downsampled data best matches query. For example, if the raw data is pre-downsampled based on 1m, 5m, 8m, and 15m and the downsampling interval specified in a query is 10m, the data pre-downsampled based on 5m best matches the query.
In no pre-downsampled data matches the downsampling interval specified in a query, the raw data is queried.
Query methods
You can use SQL statements or methods that are compatible with Time Series Database (TSDB) to query pre-downsampled data. We recommend that you use SQL statements to query pre-downsampled data. If your application is built based on OpenTSDB or TSDB, you can use methods that are compatible with TSDB to query pre-downsampled data.
Use SQL statements to query pre-downsampled data
By default, when you query pre-downsampled data, the raw data before pre-downsampling is queried. To query only pre-downsampled data, you must add the /*+ PREDOWNSAMPLE */ hint in the query statements.
SELECT /*+ PREDOWNSAMPLE */ sum(temperature) from sensor
WHERE time >= 1619074800000 AND time <= 1619085600000
SAMPLE BY 1h;
Use TSDB-compatible methods to query pre-downsampled data
You can use API-based multi-value queries, API-based single-value queries, and SDKs to query pre-downsampled data.
Use SDKs to query pre-downsampled data
When you use a SDK to query pre-downsampled data, you must configure the DownsampleDataSource parameter in the MultiFieldSubQuery builder.
Example
long startTime = 1619074800000L;
long endTime = 1619085600000L;
final String metric = "wind";
final String field = "speed";
MultiFieldSubQueryDetails fieldSubQueryDetails = MultiFieldSubQueryDetails
.field(field)
.aggregator(Aggregator.SUM)
.downsample("1h-sum")
.build();
MultiFieldSubQuery subQuery = MultiFieldSubQuery
.metric(metric)
.fieldsInfo(fieldSubQueryDetails)
// Query pre-downsampled data.
.downsampleDataSource(DownsampleDataSource.DOWNSAMPLE)
.build();
MultiFieldQuery query = MultiFieldQuery.
start(startTime).
end(endTime).
sub(subQuery).
build();
List<MultiFieldQueryResult> result = tsdb.multiFieldQuery(query);
Parameters
You can set the DownsampleDataSource parameter to one of the following values:
DOWNSAMPLE: Query the pre-downsampled data.
RAW: Query the raw data. The default value of this parameter is RAW.
For more information about how to use SDK multi-value data model to query pre-downsampled data, see Use the SDK multi-value data model to read data.
Query pre-downsampled data by using API-based multi-value queries
When you use API-based multi-value queries to query pre-downsampled data, you must add the downsampleSource parameter and set it to downsample. For more information, see Query a multivariate data point.
Request path and method
Request path | Request method | Description |
/api/mquery | GET | Query pre-downsampled data. |
Request parameters
Parameter | Required | Description | Example |
downsampleSource | No | The data source that is queried. Valid value:
| downsample |
Sample request
POST /api/mquery
{
"start": 1346846400000,
"end": 1346846402000,
"queries": [
{
"downsampleDataSource" : "downsample",
"metric": "wind",
"fields" : [
{
"field" : "speed",
"aggregator" : "none",
"downsample" : "1h-sum"
}
],
"tags":{
"sensor":"IOTE_8859_0002"
}
}
]
}
Best practices for pre-downsampling
Pre-downsampling for data sampled at an interval of seconds
For data that is sampled at an interval of seconds, we recommend that you configure pre-downsampling rules based on intervals of one minute, one hour, and one day.
Sample pre-downsampling rules
Configure pre-downsampling rules in which the sum, count, min and max aggregate functions are specified based on intervals of one minute, one hour, and one day.
CREATE PREDOWNSAMPLE `1m` AGGREGATORS (`sum`, `count`, `min`, `max`) ON sensor;
CREATE PREDOWNSAMPLE `1h` AGGREGATORS (`sum`, `count`, `min`, `max`) ON sensor;
CREATE PREDOWNSAMPLE `1d` AGGREGATORS (`sum`, `count`, `min`, `max`) ON sensor;
Sample pre-downsampling queries
Perform a downsampling query at a downsampling ratio of any minute by using the pre-dowmsampling rule configured based on an interval of one minute.
SELECT /*+ PREDOWNSAMPLE */ sum(temperature), max(temperature) FROM sensor WHERE time >= 1619074800000 AND time <= 1619085600000 SAMPLE BY <N>m;
Perform a downsampling query at a downsampling ratio of any hour by using the pre-dowmsampling rule configured based on an interval of one hour.
SELECT /*+ PREDOWNSAMPLE */ sum(temperature), max(temperature) FROM sensor WHERE time >= 1619074800000 AND time <= 1619085600000 SAMPLE BY <N>h;
Perform a downsampling query at a downsampling ratio of any day by using the pre-dowmsampling rule configured based on an interval of one day.
SELECT /*+ PREDOWNSAMPLE */ sum(temperature), max(temperature) FROM sensor WHERE time >= 1619074800000 AND time <= 1619600400000 SAMPLE BY <N>d;
Pre-downsampling for data sampled at an interval of minutes
For data that is sampled at an interval of minutes, we recommend that you configure pre-downsampling rules based on intervals of one hour and one day to perform a downsampling query at a downsampling ratio of any hour or day.
Sample pre-downsampling rules
Configure pre-downsampling rules in which the sum, count, min and max aggregate functions are specified based on intervals of one hour and one day.
CREATE PREDOWNSAMPLE `1h` AGGREGATORS (`sum`, `count`, `min`, `max`) ON sensor;
CREATE PREDOWNSAMPLE `1d` AGGREGATORS (`sum`, `count`, `min`, `max`) ON sensor;
Sample pre-downsampling queries
Perform a downsampling query at a downsampling ratio of any hour by using the pre-dowmsampling rule configured based on an interval of one hour.
SELECT /*+ PREDOWNSAMPLE */ sum(temperature), max(temperature) FROM sensor WHERE time >= 1619074800000 AND time <= 1619085600000 SAMPLE BY <N>h;
Perform a downsampling query at a downsampling ratio of any day by using the pre-dowmsampling rule configured based on an interval of one day.
SELECT /*+ PREDOWNSAMPLE */ sum(temperature), max(temperature) FROM sensor WHERE time >= 1619074800000 AND time <= 1619600400000 SAMPLE BY <N>d;