Available in: Redis Stack
Time complexity: O(n/m+k) where n = Number of data points, m = Chunk size (data points per chunk), k = Number of data points that are in the requested ranges
Query a range across multiple time series by filters in reverse direction.
TS.MREVRANGE fromTimestamp toTimestamp
[FILTER_BY_TS TS...]
[FILTER_BY_VALUE min max]
[WITHLABELS | SELECTED_LABELS label...]
[COUNT count]
[[ALIGN value] AGGREGATION aggregator bucketDuration [BUCKETTIMESTAMP bt] [EMPTY]]
FILTER filter..
[GROUPBY label REDUCE reducer]
fromTimestamp - Start timestamp for the range query. -
can be used to express the minimum possible timestamp (0).
toTimestamp - End timestamp for range query, +
can be used to express the maximum possible timestamp.
FILTER filter...
This is the list of possible filters:
=
value - label equals value!=
value - label doesn't equal value=
- key does not have the label label!=
- key has label label=(
value1,
value2,
...)
- key with label label that equals one of the values in the list!=(
value1,
value2,
...)
- key with label label that doesn't equal any of the values in the listNote: Whenever filters need to be provided, a minimum of one label=
value filter must be applied.
Optional parameters:
FILTER_BY_TS
ts... - Followed by a list of timestamps to filter the result by specific timestamps
FILTER_BY_VALUE
min max - Filter result by value using minimum and maximum.
WITHLABELS
- Include in the reply all label-value pairs representing metadata labels of the time series.
If WITHLABELS
or SELECTED_LABELS
are not specified, by default, an empty list is reported as the label-value pairs.
SELECTED_LABELS
label... - Include in the reply a subset of the label-value pairs that represent metadata labels of the time series. This is usefull when there is a large number of labels per series, but only the values of some of the labels are required.
If WITHLABELS
or SELECTED_LABELS
are not specified, by default, an empty list is reported as the label-value pairs.
COUNT
count - Maximum number of returned samples per time series.
ALIGN
value - Time bucket alignment control for AGGREGATION. This will control the time bucket timestamps by changing the reference timestamp on which a bucket is defined.
Possible values:
start
or -
: The reference timestamp will be the query start interval time (fromTimestamp
) which can't be -
end
or +
: The reference timestamp will be the query end interval time (toTimestamp
) which can't be +
0
AGGREGATION
aggregator bucketDuration
Aggregate results into time buckets.
aggregator | description |
---|---|
avg | arithmetic mean of all values |
sum | sum of all values |
min | minimum value |
max | maximum value |
range | difference between the highest and the lowest value |
count | number of values |
first | the value with the lowest timestamp in the bucket |
last | the value with the highest timestamp in the bucket |
std.p | population standard deviation of the values |
std.s | sample standard deviation of the values |
var.p | population variance of the values |
var.s | sample variance of the values |
twa | time-weighted average of all values |
The alignment of time buckets is 0.
GROUPBY
label REDUCE
reducer
Aggregate results across different time series, grouped by the provided label name.
When combined with AGGREGATION
the groupby/reduce is applied post aggregation stage.
reducer | description |
---|---|
avg | per label value: arithmetic mean of all values |
sum | per label value: sum of all values |
min | per label value: minimum value |
max | per label value: maximum value |
range | per label value: difference between the highest and the lowest value |
count | per label value: number of values |
std.p | per label value: population standard deviation of the values |
std.s | per label value: sample standard deviation of the values |
var.p | per label value: population variance of the values |
var.s | per label value: sample variance of the values |
<label>=<groupbyvalue>
__reducer__
: the reducer used__source__
: the time series keys used to compute the grouped series ("key1,key2,key3,...")For each time series matching the specified filters, the following is reported:
WITHLABELS
is specified, all labels associated with this time series are reportedSELECTED_LABELS
label... is specified, the selected labels are reportedNote: MREVRANGE command can't be part of transaction when running on Redis cluster.
127.0.0.1:6379> TS.MRANGE 1548149180000 1548149210000 AGGREGATION avg 5000 FILTER area_id=32 sensor_id!=1
1) 1) "temperature:2:32"
2) (empty list or set)
3) 1) 1) (integer) 1548149180000
2) "27.600000000000001"
2) 1) (integer) 1548149185000
2) "23.800000000000001"
3) 1) (integer) 1548149190000
2) "24.399999999999999"
4) 1) (integer) 1548149195000
2) "24"
5) 1) (integer) 1548149200000
2) "25.600000000000001"
6) 1) (integer) 1548149205000
2) "25.800000000000001"
7) 1) (integer) 1548149210000
2) "21"
2) 1) "temperature:3:32"
2) (empty list or set)
3) 1) 1) (integer) 1548149180000
2) "26.199999999999999"
2) 1) (integer) 1548149185000
2) "27.399999999999999"
3) 1) (integer) 1548149190000
2) "24.800000000000001"
4) 1) (integer) 1548149195000
2) "23.199999999999999"
5) 1) (integer) 1548149200000
2) "25.199999999999999"
6) 1) (integer) 1548149205000
2) "28"
7) 1) (integer) 1548149210000
2) "20"
127.0.0.1:6379> TS.MRANGE 1548149180000 1548149210000 AGGREGATION avg 5000 WITHLABELS FILTER area_id=32 sensor_id!=1
1) 1) "temperature:2:32"
2) 1) 1) "sensor_id"
2) "2"
2) 1) "area_id"
2) "32"
3) 1) 1) (integer) 1548149180000
2) "27.600000000000001"
2) 1) (integer) 1548149185000
2) "23.800000000000001"
3) 1) (integer) 1548149190000
2) "24.399999999999999"
4) 1) (integer) 1548149195000
2) "24"
5) 1) (integer) 1548149200000
2) "25.600000000000001"
6) 1) (integer) 1548149205000
2) "25.800000000000001"
7) 1) (integer) 1548149210000
2) "21"
2) 1) "temperature:3:32"
2) 1) 1) "sensor_id"
2) "3"
2) 1) "area_id"
2) "32"
3) 1) 1) (integer) 1548149180000
2) "26.199999999999999"
2) 1) (integer) 1548149185000
2) "27.399999999999999"
3) 1) (integer) 1548149190000
2) "24.800000000000001"
4) 1) (integer) 1548149195000
2) "23.199999999999999"
5) 1) (integer) 1548149200000
2) "25.199999999999999"
6) 1) (integer) 1548149205000
2) "28"
7) 1) (integer) 1548149210000
2) "20"
127.0.0.1:6379> TS.ADD ts1 1548149180000 90 labels metric cpu metric_name system
(integer) 1
127.0.0.1:6379> TS.ADD ts1 1548149185000 45
(integer) 2
127.0.0.1:6379> TS.ADD ts2 1548149180000 99 labels metric cpu metric_name user
(integer) 2
127.0.0.1:6379> TS.MRANGE - + WITHLABELS FILTER metric=cpu GROUPBY metric_name REDUCE max
1) 1) "metric_name=system"
2) 1) 1) "metric_name"
2) "system"
2) 1) "__reducer__"
2) "max"
3) 1) "__source__"
2) "ts1"
3) 1) 1) (integer) 1548149180000
2) 90
2) 1) (integer) 1548149185000
2) 45
2) 1) "metric_name=user"
2) 1) 1) "metric_name"
2) "user"
2) 1) "__reducer__"
2) "max"
3) 1) "__source__"
2) "ts2"
3) 1) 1) (integer) 1548149180000
2) 99
127.0.0.1:6379> TS.ADD ts1 1548149180000 90 labels metric cpu metric_name system
(integer) 1
127.0.0.1:6379> TS.ADD ts1 1548149185000 45
(integer) 2
127.0.0.1:6379> TS.ADD ts2 1548149180000 99 labels metric cpu metric_name user
(integer) 2
127.0.0.1:6379> TS.MRANGE - + FILTER_BY_VALUE 90 100 WITHLABELS FILTER metric=cpu
1) 1) "ts1"
2) 1) 1) "metric"
2) "cpu"
2) 1) "metric_name"
2) "system"
3) 1) 1) (integer) 1548149180000
2) 90
2) 1) "ts2"
2) 1) 1) "metric"
2) "cpu"
2) 1) "metric_name"
2) "user"
3) 1) 1) (integer) 1548149180000
2) 99
127.0.0.1:6379> TS.ADD ts1 1548149180000 90 labels metric cpu metric_name system team NY
(integer) 1
127.0.0.1:6379> TS.ADD ts1 1548149185000 45
(integer) 2
127.0.0.1:6379> TS.ADD ts2 1548149180000 99 labels metric cpu metric_name user team SF
(integer) 2
127.0.0.1:6379> TS.MRANGE - + SELECTED_LABELS team FILTER metric=cpu
1) 1) "ts1"
2) 1) 1) "team"
2) "NY"
3) 1) 1) (integer) 1548149180000
2) 90
2) 1) (integer) 1548149185000
2) 45
2) 1) "ts2"
2) 1) 1) "team"
2) "SF"
3) 1) 1) (integer) 1548149180000
2) 99