3

First post here and new to InfluxDB.

I have been given the following query, and I have no clue how I can optimize it.

A period of 24 hours and 1m windows takes around 2.4 seconds at the moment (is this the expected amount of time).

I suspect one of the reasons is that there are 4 tables (querying the same set of data) and 3 joins.

I have looked into the map function to try and reduce it to one table but I can't seem to get it to work with the window.

  bucketName = "${bucket}"
  startTime = -${period}
  interval = ${interval}
  token = "${token}"
  
  minPrice = from (bucket: bucketName)
    |> range(start: startTime, stop: now())
    |> filter(fn: (r) => r["_field"] == token)
    |> window(every: interval)
    |> min()
    |> duplicate(column: "_value", as: "low")
    |> keep(columns: ["low", "_start", "_stop"] )
  
  maxPrice = from (bucket: bucketName)
    |> range(start: startTime, stop: now())
    |> filter(fn: (r) => r["_field"] == token)
    |> window(every: interval)
    |> max()
    |> duplicate(column: "_value", as: "high")
    |> keep(columns: ["high", "_start", "_stop"] )
  
  openPrice = from (bucket: bucketName)
    |> range(start: startTime, stop: now())
    |> filter(fn: (r) => r["_field"] == token)
    |> window(every: interval)
    |> first()
    |> duplicate(column: "_value", as: "open")
    |> keep(columns: ["open", "_stop", "_start"] )
  
  closePrice = from (bucket: bucketName)
    |> range(start: startTime, stop: now())
    |> filter(fn: (r) => r["_field"] == token)
    |> window(every: interval)
    |> last()
    |> duplicate(column: "_value", as: "close")
    |> keep(columns: ["close", "_stop", "_start"] )
  
  highLowData = join(tables: {min: minPrice, max: maxPrice}, on: ["_start", "_stop"])
  openCloseData = join(tables: {open: openPrice, close: closePrice}, on:  ["_start", "_stop"])
  join(tables: {highLow: highLowData, openClose: openCloseData}, on:  ["_start", "_stop"])

I have managed to optimize it down to 0.7s by using a union rather than a join. However now I'm faced with data that has empty fields.

Like this:

Like this

Query below

startTime = -24h
breakDown = 1m
token = "tokenName"

all = from (bucket: "prices")
  |> range(start: startTime, stop: now())
  |> filter(fn: (r) => r["_field"] == token)
  |> window(every: breakDown)

lowPrice = all
  |> min()
  |> duplicate(column: "_value", as: "low")
  |> keep(columns: ["low", "_stop", "_start"] )

highPrice = all
  |> max()
  |> duplicate(column: "_value", as: "high")
  |> keep(columns: ["high", "_stop", "_start"] )

openPrice = all
  |> first()
  |> duplicate(column: "_value", as: "open")
  |> keep(columns: ["open", "_stop", "_start"] )

closePrice = all
  |> last()
  |> duplicate(column: "_value", as: "close")
  |> keep(columns: ["close", "_stop", "_start"] )

highLowData = union(tables: [lowPrice, highPrice])
openCloseData = union(tables: [openPrice, closePrice])
result = union(tables: [highLowData, openCloseData])
|> yield (name: "Result")
bad_coder
  • 11,289
  • 20
  • 44
  • 72
Lou
  • 31
  • 3

0 Answers0