This topic uses a data center performance monitoring scenario. It uses sample data generated by a tool to show how to use TSQL to run time series queries.
Sample data
The sample data comes from the sample data generator in benchmark (https://github.com/influxdata/influxdb-comparisons), a time series performance test tool. After you install and compile the tool, run the following two steps to generate the data and load it into the TSDB engine.
Generate data
cd influxdb-comparisons/cmd
bulk_data_gen/bulk_data_gen --seed=123 --use-case=devops --scale-var=10 --format=opentsdb --timestamp-start="2019-03-01T00:00:00Z" --timestamp-end="2019-03-01T00:10:00Z" > tsdb_devops_sf10_10m_seed123.jsonThe following shows part of the sample data:
{"metric":"redis.evicted_keys","timestamp":1551398990000,"tags":{"arch":"x86","datacenter":"us-east-1b","hostname":"host_9","os":"Ubuntu16.10","port":"1470","rack":"7","region":"us-east-1","server":"redis_29176","service":"14","service_environment":"production","service_version":"0","team":"LON"},"value":2951}
{"metric":"redis.keyspace_hits","timestamp":1551398990000,"tags":{"arch":"x86","datacenter":"us-east-1b","hostname":"host_9","os":"Ubuntu16.10","port":"1470","rack":"7","region":"us-east-1","server":"redis_29176","service":"14","service_environment":"production","service_version":"0","team":"LON"},"value":2945}
{"metric":"redis.keyspace_misses","timestamp":1551398990000,"tags":{"arch":"x86","datacenter":"us-east-1b","hostname":"host_9","os":"Ubuntu16.10","port":"1470","rack":"7","region":"us-east-1","server":"redis_29176","service":"14","service_environment":"production","service_version":"0","team":"LON"},"value":2944}
{"metric":"redis.instantaneous_ops_per_sec","timestamp":1551398990000,"tags":{"arch":"x86","datacenter":"us-east-1b","hostname":"host_9","os":"Ubuntu16.10","port":"1470","rack":"7","region":"us-east-1","server":"redis_29176","service":"14","service_environment":"production","service_version":"0","team":"LON"},"value":65}
{"metric":"redis.instantaneous_input_kbps","timestamp":1551398990000,"tags":{"arch":"x86","datacenter":"us-east-1b","hostname":"host_9","os":"Ubuntu16.10","port":"1470","rack":"7","region":"us-east-1","server":"redis_29176","service":"14","service_environment":"production","service_version":"0","team":"LON"},"value":58}Load data
cat tsdb_devops_sf10_10m_seed123.json | bulk_load_opentsdb/bulk_load_opentsdb --urls=http://your_tsdb_host:port_num -workers=5Queries
Time range query: return all columns of a metric within a time range, including the value, the timestamp, and the values of the corresponding tag keys.
select * from tsdb.`cpu.usage_system` where `timestamp` between '2019-03-01 00:00:00' and '2019-03-01 00:00:10'Time range query: return specified columns of a metric within a time range, including the value, the timestamp, and the values of specific tag keys.
select `value`, `timestamp`, hostname, datacenter from tsdb.`cpu.usage_system` where `timestamp` between '2019-03-01 00:00:00' and '2019-03-01 00:00:10'Time range and tag key conditional query: return the value and the timestamp of a metric within a time range, where hostname matches an IN list.
select `value`, `timestamp`, hostname, datacenter from tsdb.`cpu.usage_system` where `timestamp` between '2019-03-01 00:00:00' and '2019-03-01 00:00:10' and hostname in ('host_0', 'host_2', 'host_4')Sort query results by timestamp: return the value, the timestamp, and the values of the corresponding tag keys of a metric within a time range.
select * from tsdb.`cpu.usage_system` where `timestamp` between '2019-03-01 00:00:00' and '2019-03-01 00:00:10' order by `timestamp`Conditional query with a value filter that uses a mathematical expression: return the value, the timestamp, and the values of the corresponding tag keys of a metric within a time range, where the square root of the value is greater than 1.5.
select * from tsdb.`cpu.usage_system` where `timestamp` between '2019-03-01 00:00:00' and '2019-03-01 00:00:10' and sqrt(`value`) > 1.5Grouping and aggregation query: group the data by hostname and datacenter, and compute the maximum, minimum, and average value of each group.
select hostname, datacenter, max(`value`) as maxV, min(`value`) as minV, avg(`value`) as avgV from tsdb.`cpu.usage_system` where `timestamp` between '2019-03-01 00:00:00' and '2019-03-01 00:00:10' group by hostname, datacenterTime-window aggregate query with grouping: group the data by hostname and datacenter, group it further into 2-minute intervals, and compute the maximum, minimum, and average value.
select hostname, datacenter, tumble(`timestamp`, interval '2' minute) as ts, max(`value`) as maxV, min(`value`) as minV, avg(`value`) as avgV from tsdb.`cpu.usage_system` where `timestamp` between '2019-03-01 00:00:00' and '2019-03-01 00:10:00' group by hostname, datacenter, tsTime-window aggregate query with grouping: compute the value of the aggregated expression
max(value) - min(value) + 0.5* avg(value).select hostname, datacenter, tumble(`timestamp`, interval '2' minute) as ts, max(`value`) - min(`value`) + 0.5* avg(`value`) as compV from tsdb.`cpu.usage_system` where `timestamp` between '2019-03-01 00:00:00' and '2019-03-01 00:10:00' group by hostname, datacenter, tsCompute the difference between the values of two adjacent timestamps on each host.
The following example uses the
window function lag(). Thelag()function uses a window frame that is partitioned by hostname and ordered by timestamp, and returns the value of the record that precedes the current record in the same window. The difference between the two values is the difference between the values of adjacent timestamps on each host.select hostname, `timestamp`, `value`, `value` - lag(`value`) over(partition by hostname order by `timestamp`) as diff from tsdb.`cpu.usage_system` where `timestamp` between '2019-03-01' and '2019-03-01 00:10:00'Compute the difference between the values of two adjacent timestamps on each host, and reset the difference to 0 if it exceeds the outlier threshold.
The following example puts the previous query in a subquery and uses a case expression outside the subquery. If the difference in the query exceeds 50.0, which is considered an outlier, the difference is reset to 0.0.
select hostname, `timestamp`, `value`, case when diff > 50.0 then 0.0 else diff end from ( select hostname, `timestamp`, `value`, `value` - lag(`value`) over(partition by hostname order by `timestamp`) as diff from tsdb.`cpu.usage_system` where `timestamp` between '2019-03-01' and '2019-03-01 00:10:00' );Compute the maximum value of the records within one minute on each host, and the difference between the maximum values of adjacent minutes.
Unlike the previous difference calculation, this query returns the difference between the maximum values of each minute. Compute the maximum value of each minute in the subquery, and use the
lag()window function outside the subquery to get the difference between the maximum values of adjacent minutes.select hostname, ts, maxValue, maxValue - lag(maxValue) over(partition by hostname order by ts) as diff from ( select hostname, tumble(`timestamp`, interval '1' minute) ts, max(`value`) maxValue from tsdb.`cpu.usage_system` where `timestamp` between '2019-03-01' and '2019-03-01 00:10:00' group by hostname, ts)