Efficient Data Querying in TDengine

TDengine
4 min readSep 19, 2021

TDengine is a time-series database designed and optimized for IoT. You can get it on GitHub. This blog will introduce the efficient data querying in TDengine.

Main Query Features

TDengine uses SQL as the query language. Applications can send SQL statements through C/C++, Java, Go, C#, Python, Node.js connectors, and users can manually execute SQL Ad-Hoc Query through the Command Line Interface (CLI) tool TAOS Shell provided by TDengine. TDengine supports the following query functions:

  • Single-column and multi-column data query
  • Multiple filters for tags and numeric values: >, <, =, < >, like, etc
  • Group by, Order by, Limit/Offset of aggregation results
  • Four operations for numeric columns and aggregation results
  • Time stamp aligned join query (implicit join) operations
  • Multiple aggregation/calculation functions: count, max, min, avg, sum, twa, stddev, leastsquares, top, bottom, first, last, percentile, apercentile, last_row, spread, diff, etc

For example, in TAOS shell, the records with vlotage > 215 are queried from table d1001, sorted in descending order by timestamps, and only two records are outputted.

In order to meet the needs of an IoT scenario, TDengine supports several special functions, such as twa (time weighted average), spread (difference between maximum and minimum), last_row (last record), etc. More functions related to IoT scenarios will be added. TDengine also supports continuous queries.

For specific query syntax, please see the Data Query section of TAOS SQL.

Multi-table Aggregation Query

In an IoT scenario, there are often multiple data collection points in a same type. TDengine uses the concept of STable to describe a certain type of data collection point, and an ordinary table to describe a specific data collection point. At the same time, TDengine uses tags to describe the static attributes of data collection points. A given data collection point has a specific tag value. By specifying the filters of tags, TDengine provides an efficient method to aggregate and query the sub-tables of STables (data collection points of a certain type). Aggregation functions and most operations on ordinary tables are applicable to STables, and the syntax is exactly the same.

Example 1: In TAOS Shell, look up the average voltages collected by all smart meters in Beijing and group them by location

Example 2: In TAOS Shell, look up the number of records with groupId 2 in the past 24 hours, check the maximum current of all smart meters

TDengine only allows aggregation queries between tables belonging to a same STable, means aggregation queries between different STables are not supported. In the Data Query section of TAOS SQL, query class operations will all be indicated that whether STables are supported.

Down Sampling Query, Interpolation

In a scenario of IoT, it is often necessary to aggregate the collected data by intervals through down sampling. TDengine provides a simple keyword interval, which makes query operations according to time windows extremely simple. For example, the current values collected by smart meter d1001 are summed every 10 seconds.

The down sampling operation is also applicable to STables, such as summing the current values collected by all smart meters in Beijing every second.

The down sampling operation also supports time offset, such as summing the current values collected by all smart meters every second, but requires each time window to start from 500 milliseconds.

In IoT scenario, it is difficult to synchronize the time stamp of collected data at each point, but many analysis algorithms (such as FFT) need to align the collected data strictly at equal intervals of time. In many systems, it’s required to write their own programs to process, but the down sampling operation of TDengine can be used to solve the problem easily. If there is no collected data in an interval, TDengine also provides interpolation calculation function.

For details of syntax rules, please refer to the Time-dimension Aggregation section of TAOS SQL.

--

--

TDengine

Next generation data historian purpose-built for Industry 4.0 and Industrial IoT. See more at https://tdengine.com