TS.RANGE key fromTimestamp toTimestamp [FILTER_BY_TS Timestamp [Timestamp ...]] [ FILTER_BY_VALUE min max] [COUNT count] [ [ALIGN value] AGGREGATION AVG | FIRST | LAST | MIN | MAX | SUM | RANGE | COUNT | STD.P | STD.S | VAR.P | VAR.S | TWA bucketDuration [BUCKETTIMESTAMP] [EMPTY]]

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 range

TS.RANGE

Query a range in forward direction.

TS.RANGE key fromTimestamp toTimestamp
         [FILTER_BY_TS ts...]
         [FILTER_BY_VALUE min max]
         [COUNT count] 
         [[ALIGN value] AGGREGATION aggregator bucketDuration [BUCKETTIMESTAMP bt] [EMPTY]]
  • key - Key name for time series
  • 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.

Optional parameters:

  • FILTER_BY_TS ts... - a list of timestamps to filter the result by specific timestamps

  • FILTER_BY_VALUE min max - Filter result by value using minimum and maximum.

  • COUNT count - Maximum number of returned samples.

  • 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 +
    • A specific timestamp: align the reference timestamp to a specific time
    • Note: when not provided, alignment is set to 0
  • AGGREGATION aggregator bucketDuration

    Aggregate results into time buckets.

    • aggregator - Aggregation type: One of the following:
      aggregatordescription
      avgarithmetic mean of all values
      sumsum of all values
      minminimum value
      maxmaximum value
      rangedifference between the highest and the lowest value
      countnumber of values
      firstthe value with the lowest timestamp in the bucket
      lastthe value with the highest timestamp in the bucket
      std.ppopulation standard deviation of the values
      std.ssample standard deviation of the values
      var.ppopulation variance of the values
      var.ssample variance of the values
      twatime-weighted average of all values
    • bucketDuration - duration of each bucket, in milliseconds

    The alignment of time buckets is 0.

Complexity

TS.RANGE complexity is O(n/m+k).

n = Number of data points m = Chunk size (data points per chunk) k = Number of data points that are in the requested range

This can be improved in the future by using binary search to find the start of the range, which makes this O(Log(n/m)+k*m). But because m is pretty small, we can neglect it and look at the operation as O(Log(n) + k).

Aggregated Query Example

127.0.0.1:6379> TS.RANGE temperature:3:32 1548149180000 1548149210000 AGGREGATION avg 5000
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"