Flux vs InfluxQL

This page documents an earlier version of InfluxDB. InfluxDB v2.7 is the latest stable version. View this page in the v2.7 documentation.

Flux is an alternative to InfluxQL and other SQL-like query languages for querying and analyzing data. Flux uses functional language patterns that overcome many InfluxQL limitations. Check out the following distinctions between Flux and InfluxQL:

Tasks possible with Flux

Joins

InfluxQL has never supported joins. Although you can use a join in a TICKscript, TICKscript’s join capabilities are limited. Flux’s join() function lets you join data from any bucket, any measurement, and on any columns as long as each data set includes the columns to join on.

  1. dataStream1 = from(bucket: "example-bucket1")
  2. |> range(start: -1h)
  3. |> filter(fn: (r) => r._measurement == "network" and r._field == "bytes-transferred")
  4. dataStream2 = from(bucket: "example-bucket2")
  5. |> range(start: -1h)
  6. |> filter(fn: (r) => r._measurement == "httpd" and r._field == "requests-per-sec")
  7. join(tables: {d1: dataStream1, d2: dataStream2}, on: ["_time", "_stop", "_start", "host"])

For an in-depth walkthrough of using the join() function, see how to join data with Flux.

Math across measurements

Being able to perform joins across measurements lets you calculate data from separate measurements. The example below takes data from two measurements, mem and processes, joins them, and then calculates the average amount of memory used per running process:

  1. // Memory used (in bytes)
  2. memUsed = from(bucket: "example-bucket")
  3. |> range(start: -1h)
  4. |> filter(fn: (r) => r._measurement == "mem" and r._field == "used")
  5. // Total processes running
  6. procTotal = from(bucket: "example-bucket")
  7. |> range(start: -1h)
  8. |> filter(fn: (r) => r._measurement == "processes" and r._field == "total")
  9. // Join memory used with total processes to calculate
  10. // the average memory (in MB) used for running processes.
  11. join(tables: {mem: memUsed, proc: procTotal}, on: ["_time", "_stop", "_start", "host"])
  12. |> map(fn: (r) => ({_time: r._time, _value: r._value_mem / r._value_proc / 1000000}))

Sort by tags

InfluxQL’s sorting capabilities only let you control the sort order of time using the ORDER BY time clause. The Flux sort() function sorts records based on a list of columns. Depending on the column type, Flux sorts records lexicographically, numerically, or chronologically.

  1. from(bucket: "example-bucket")
  2. |> range(start: -12h)
  3. |> filter(fn: (r) => r._measurement == "system" and r._field == "uptime")
  4. |> sort(columns: ["region", "host", "_value"])

Group by any column

InfluxQL lets you group by tags or time intervals only. Flux lets you group data by any column, including _value. Use the Flux group() function to define which columns to group data by.

  1. from(bucket:"example-bucket")
  2. |> range(start: -12h)
  3. |> filter(fn: (r) => r._measurement == "system" and r._field == "uptime" )
  4. |> group(columns:["host", "_value"])

Window by calendar months and years

InfluxQL does not support windowing data by calendar months and years due to their varied lengths. Flux supports calendar month and year duration units (1mo, 1y) and lets you window and aggregate data by calendar month and year.

  1. from(bucket:"example-bucket")
  2. |> range(start:-1y)
  3. |> filter(fn: (r) => r._measurement == "mem" and r._field == "used_percent" )
  4. |> aggregateWindow(every: 1mo, fn: mean)

Work with multiple data sources

InfluxQL can only query data stored in InfluxDB. Flux can query data from other data sources such as CSV, PostgreSQL, MySQL, Google BigTable, and more. Join that data with data in InfluxDB to enrich query results.

  1. import "csv"
  2. import "sql"
  3. csvData = csv.from(csv: rawCSV)
  4. sqlData = sql.from(
  5. driverName: "postgres",
  6. dataSourceName: "postgresql://user:password@localhost",
  7. query: "SELECT * FROM example_table",
  8. )
  9. data = from(bucket: "example-bucket")
  10. |> range(start: -24h)
  11. |> filter(fn: (r) => r._measurement == "sensor")
  12. auxData = join(tables: {csv: csvData, sql: sqlData}, on: ["sensor_id"])
  13. enrichedData = join(tables: {data: data, aux: auxData}, on: ["sensor_id"])
  14. enrichedData
  15. |> yield(name: "enriched_data")

For an in-depth walkthrough of querying SQL data, see Query SQL data sources.

DatePart-like queries

InfluxQL doesn’t support DatePart-like queries that only return results during specified hours of the day. The Flux hourSelection function returns only data with time values in a specified hour range.

  1. from(bucket: "example-bucket")
  2. |> range(start: -1h)
  3. |> filter(fn: (r) => r._measurement == "cpu" and r.cpu == "cpu-total")
  4. |> hourSelection(start: 9, stop: 17)

Pivot

Pivoting data tables isn’t supported in InfluxQL. Use the Flux pivot() function to pivot data tables by rowKey, columnKey, and valueColumn parameters.

  1. from(bucket: "example-bucket")
  2. |> range(start: -1h)
  3. |> filter(fn: (r) => r._measurement == "cpu" and r.cpu == "cpu-total")
  4. |> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")

Histograms

Generating histograms isn’t supported in InfluxQL. Use the Flux histogram() function to generate a cumulative histogram.

  1. from(bucket: "example-bucket")
  2. |> range(start: -1h)
  3. |> filter(fn: (r) => r._measurement == "mem" and r._field == "used_percent")
  4. |> histogram(buckets: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100])

For more examples, see how to create histograms with Flux.

Covariance

Flux provides functions for simple covariance calculations. Use the covariance() function to calculate the covariance between two columns and the cov() function to calculate the covariance between two data streams.

Covariance between two columns
  1. from(bucket: "example-bucket")
  2. |> range(start:-5m)
  3. |> covariance(columns: ["x", "y"])
Covariance between two streams of data
  1. table1 = from(bucket: "example-bucket")
  2. |> range(start: -15m)
  3. |> filter(fn: (r) => r._measurement == "measurement_1")
  4. table2 = from(bucket: "example-bucket")
  5. |> range(start: -15m)
  6. |> filter(fn: (r) => r._measurement == "measurement_2")
  7. cov(x: table1, y: table2, on: ["_time", "_field"])

Cast booleans to integers

InfluxQL supports type casting for numeric data types (floats to integers and vice versa) only. Use Flux type conversion functions to perform many more type conversions, including casting boolean values to integers.

Cast boolean field values to integers
  1. from(bucket: "example-bucket")
  2. |> range(start: -1h)
  3. |> filter(fn: (r) => r._measurement == "m" and r._field == "bool_field")
  4. |> toInt()

String manipulation and data shaping

InfluxQL doesn’t support string manipulation when querying data. Use Flux Strings package functions to operate on string data. Combine functions in this package with the map() function to perform operations like sanitizing and normalizing strings.

  1. import "strings"
  2. from(bucket: "example-bucket")
  3. |> range(start: -1h)
  4. |> filter(fn: (r) => r._measurement == "weather" and r._field == "temp")
  5. |> map(
  6. fn: (r) => ({
  7. r with
  8. location: strings.toTitle(v: r.location),
  9. sensor: strings.replaceAll(v: r.sensor, t: " ", u: "-"),
  10. status: strings.substring(v: r.status, start: 0, end: 8)
  11. })
  12. )

Work with geo-temporal data

InfluxQL doesn’t support working with geo-temporal data. The Flux Geo package is a collection of functions that let you shape, filter, and group geo-temporal data.

  1. import "experimental/geo"
  2. from(bucket: "geo/autogen")
  3. |> range(start: -1w)
  4. |> filter(fn: (r) => r._measurement == "taxi")
  5. |> geo.shapeData(latField: "latitude", lonField: "longitude", level: 20)
  6. |> geo.filterRows(region: {lat: 40.69335938, lon: -73.30078125, radius: 20.0}, strict: true)
  7. |> geo.asTracks(groupBy: ["fare-id"])

InfluxQL and Flux parity

We’re continuing to add functions to complete parity between Flux and InfluxQL. The table below shows InfluxQL statements, clauses, and functions along with their equivalent Flux functions.

For a complete list of Flux functions, view all Flux functions.

InfluxQLFlux Functions
SELECTfilter()
WHEREfilter(), range()
GROUP BYgroup()
INTOto()
ORDER BYsort()
LIMITlimit()
SLIMIT
OFFSET
SOFFSET
SHOW DATABASESbuckets()
SHOW MEASUREMENTSschema.measurements
SHOW FIELD KEYSkeys()
SHOW RETENTION POLICIESbuckets()
SHOW TAG KEYSschema.tagKeys(), schema.measurementTagKeys()
SHOW TAG VALUESschema.tagValues(), schema.measurementTagValues()
SHOW SERIES
CREATE DATABASEN/A
DROP DATABASEN/A
DROP SERIESN/A
DELETEN/A
DROP MEASUREMENTN/A
DROP SHARDN/A
CREATE RETENTION POLICYN/A
ALTER RETENTION POLICYN/A
DROP RETENTION POLICYN/A
COUNTcount()
DISTINCTdistinct()
INTEGRALintegral()
MEANmean()
MEDIANmedian()
MODEmode()
SPREADspread()
STDDEVstddev()
SUMsum()
BOTTOMbottom()
FIRSTfirst()
LASTlast()
MAXmax()
MINmin()
PERCENTILEquantile()
SAMPLEsample()
TOPtop()
ABSmath.abs()
ACOSmath.acos()
ASINmath.asin()
ATANmath.atan()
ATAN2math.atan2()
CEILmath.ceil()
COSmath.cos()
CUMULATIVE_SUMcumulativeSum()
DERIVATIVEderivative()
DIFFERENCEdifference()
ELAPSEDelapsed()
EXPmath.exp()
FLOORmath.floor()
HISTOGRAMhistogram()
LNmath.log()
LOGmath.logb()
LOG2math.log2()
LOG10math.log10()
MOVING_AVERAGEmovingAverage()
NON_NEGATIVE_DERIVATIVEderivative(nonNegative:true)
NON_NEGATIVE_DIFFERENCEdifference(nonNegative:true)
POWmath.pow()
ROUNDmath.round()
SINmath.sin()
SQRTmath.sqrt()
TANmath.tan()
HOLT_WINTERSholtWinters()
CHANDE_MOMENTUM_OSCILLATORchandeMomentumOscillator()
EXPONENTIAL_MOVING_AVERAGEexponentialMovingAverage()
DOUBLE_EXPONENTIAL_MOVING_AVERAGEdoubleEMA()
KAUFMANS_EFFICIENCY_RATIOkaufmansER()
KAUFMANS_ADAPTIVE_MOVING_AVERAGEkaufmansAMA()
TRIPLE_EXPONENTIAL_MOVING_AVERAGEtripleEMA()
TRIPLE_EXPONENTIAL_DERIVATIVEtripleExponentialDerivative()
RELATIVE_STRENGTH_INDEXrelativeStrengthIndex()