InfluxQL參考
InfluxQL是一種類似SQL的查詢語言,用于與TSDB For InfluxDB?進行交互并提供專門用于存儲和分析時序數據的功能。本文檔是Influx查詢語言(InfluxQL)的參考文檔。
符號
使用Extended Backus-Naur Form(“EBNF”)指定語法。EBNF與Go語言規范中使用的符號相同。這不是巧合,因為TSDB For InfluxDB?就是用Go語言編寫的。
Production = production_name "=" [ Expression ] "." .
Expression = Alternative { "|" Alternative } .
Alternative = Term { Term } .
Term = production_name | token [ "…" token ] | Group | Option | Repetition .
Group = "(" Expression ")" .
Option = "[" Expression "]" .
Repetition = "{" Expression "}" .
符號運算符按優先級遞增排序:
| alternation
() grouping
[] option (0 or 1 times)
{} repetition (0 to n times)
查詢表示
字符
InfluxQL是使用UTF-8編碼的Unicode文本。
newline = /* the Unicode code point U+000A */ .
unicode_char = /* an arbitrary Unicode code point except newline */ .
字母和數字
字母是ASCII字符和下劃線(U+005F)的集合,下劃線(U+005F)被認為是字母。
InfluxQL只支持十進制數字。
letter = ascii_letter | "_" .
ascii_letter = "A" … "Z" | "a" … "z" .
digit = "0" … "9" .
標識符
標識符指的是數據庫名字、保留策略名字、用戶名、measurement的名字、tag key和field key。
規則:
用雙引號括起來的標識符可以包含除換行符(new line)之外的任意unicode字符。
用雙引號括起來的標識符可以包含轉義的
"
字符(也就是,\"
)。用雙引號括起來的標識符可以包含InfluxQL關鍵字。
不帶引號的標識符必須以大寫或小寫的ASCII字符或者“_”開頭。
不帶引號的標識符只能包含ASCII字符、數字和“_”。
identifier = unquoted_identifier | quoted_identifier .
unquoted_identifier = ( letter ) { letter | digit } .
quoted_identifier = `"` unicode_char { unicode_char } `"` .
示例:
cpu
_cpu_stats
"1h"
"anything really"
"1_Crazy-1337.identifier>NAME"
關鍵字
ALL ALTER ANY AS ASC BEGIN
BY CREATE CONTINUOUS DATABASE DATABASES DEFAULT
DELETE DESC DESTINATIONS DIAGNOSTICS DISTINCT DROP
DURATION END EVERY EXPLAIN FIELD FOR
FROM GRANT GRANTS GROUP GROUPS IN
INF INSERT INTO KEY KEYS KILL
LIMIT SHOW MEASUREMENT MEASUREMENTS NAME OFFSET
ON ORDER PASSWORD POLICY POLICIES PRIVILEGES
QUERIES QUERY READ REPLICATION RESAMPLE RETENTION
REVOKE SELECT SERIES SET SHARD SHARDS
SLIMIT SOFFSET STATS SUBSCRIPTION SUBSCRIPTIONS TAG
TO USER USERS VALUES WHERE WITH
WRITE
如果您使用InfluxQL關鍵字作為標識符,您需要將每個查詢中的標識符用雙引號括起來。
關鍵字time
是一個特例。time
可以是一個連續查詢名字、數據庫名字、measurement的名字、保留策略名字、subscription的名字和用戶名。在這些情況下,不需要在查詢中用雙引號將time
括起來。time
不能是field key或tag key;TSDB For InfluxDB?拒絕寫入將time
作為field key或tag key的數據,對于這種數據寫入,TSDB For InfluxDB?會返回錯誤。請查閱FAQ獲得更多相關信息。
文字
整數
InfluxQL支持十進制的整數。目前不支持十六進制和八進制。
int_lit = ( "1" … "9" ) { digit } .
浮點數
InfluxQL支持浮點數。目前不支持指數。
float_lit = int_lit "." int_lit .
字符串
字符串必須用單引號括起來。字符串可以包含轉義的'
字符(也就是\'
)。
string_lit = `'` { unicode_char } `'` .
持續時間
持續時間(duration)指定了一段時間的長度。整數后面緊跟著(沒有空格)以下列出的一個時間單位表示duration literal。可使用混合單位指定持續時間。
持續時間單位
單位 | 含義 |
ns | 納秒(十億分之一秒) |
u或μ | 微秒(百萬分之一秒) |
ms | 毫秒(千分之一秒) |
s | 秒 |
m | 分鐘 |
h | 小時 |
d | 天 |
w | 星期 |
duration_lit = int_lit duration_unit .
duration_unit = "ns" | "u" | "μ" | "ms" | "s" | "m" | "h" | "d" | "w" .
日期和時間
與本文檔的其它部分一樣,日期和時間的格式沒有指定為EBNF,而是使用Go的日期/時間解析格式來指定,這是以InfluxQL所需格式編寫的參考日期。參考日期時間是:
InfluxQL參考日期時間:2006年1月2日下午3:04:05
time_lit = "2006-01-02 15:04:05.999999" | "2006-01-02" .
布爾值
bool_lit = TRUE | FALSE .
正則表達式
regex_lit = "/" { unicode_char } "/" .
比較:=~
匹配 !~
不匹配
InfluxQL支持使用正則表達式,當指定: SELECT子句中的field key和tag key FROM子句中的measurement WHERE子句中的tag value GROUP BY子句中的tag key 目前,InfluxQL不支持使用正則表達式匹配WHERE子句、數據庫和保留策略中的非字符串類型的field value。
查詢
查詢由一個或多個以分號分隔的語句組成。
query = statement { ";" statement } .
statement = alter_retention_policy_stmt |
create_continuous_query_stmt |
create_database_stmt |
create_retention_policy_stmt |
create_subscription_stmt |
create_user_stmt |
delete_stmt |
drop_continuous_query_stmt |
drop_database_stmt |
drop_measurement_stmt |
drop_retention_policy_stmt |
drop_series_stmt |
drop_shard_stmt |
drop_subscription_stmt |
drop_user_stmt |
explain_stmt |
explain_analyze_stmt |
grant_stmt |
kill_query_statement |
revoke_stmt |
select_stmt |
show_continuous_queries_stmt |
show_databases_stmt |
show_diagnostics_stmt |
show_field_key_cardinality_stmt |
show_field_keys_stmt |
show_grants_stmt |
show_measurement_cardinality_stmt |
show_measurement_exact_cardinality_stmt |
show_measurements_stmt |
show_queries_stmt |
show_retention_policies_stmt |
show_series_cardinality_stmt |
show_series_exact_cardinality_stmt |
show_series_stmt |
show_shard_groups_stmt |
show_shards_stmt |
show_stats_stmt |
show_subscriptions_stmt |
show_tag_key_cardinality_stmt |
show_tag_key_exact_cardinality_stmt |
show_tag_keys_stmt |
show_tag_values_stmt |
show_tag_values_cardinality_stmt |
show_users_stmt .
語句
DELETE
阿里云InfluxDB數據庫支持刪除數據的操作,但InfluxDB內核對刪除操作存在死鎖風險,可能會引起讀寫失敗,建議您避免執行刪除操作。
delete_stmt = "DELETE" ( from_clause | where_clause | from_clause where_clause ) .
示例:
DELETE FROM "cpu"
DELETE FROM "cpu" WHERE time < '2000-01-01T00:00:00Z'
DELETE WHERE time < '2000-01-01T00:00:00Z'
EXPLAIN
解析并計劃查詢,然后打印查詢預計開銷的摘要。
很多SQL引擎使用EXPLAIN語句來顯示join順序、join算法以及謂詞和表達式下推(predicate and expression pushdown)。由于InfluxQL不支持join,一個InfluxQL查詢的開銷通常是一個關于訪問的總時間序列、訪問TSM文件的迭代器數量和需要掃描的TSM block的數量的函數。
EXPLAIN查詢計劃的內容包括:
表達式(expression)
輔助field(auxiliary fields)
shard的數量(number of shards)
序列的數量(number of series)
緩存的值(cached values)
文件的數量(number of files)
block的數量(number of blocks)
block的大小(size of blocks)
示例:
> explain select sum(pointReq) from "_internal"."monitor"."write" group by hostname;
> QUERY PLAN
------
EXPRESSION: sum(pointReq::integer)
NUMBER OF SHARDS: 2
NUMBER OF SERIES: 2
CACHED VALUES: 110
NUMBER OF FILES: 1
NUMBER OF BLOCKS: 1
SIZE OF BLOCKS: 931
EXPLAIN ANALYZE
執行查詢并計算運行時的實際開銷。
explain_analyze_stmt = "EXPLAIN ANALYZE" select_stmt .
示例:
> explain analyze select sum(pointReq) from "_internal"."monitor"."write" group by hostname;
> EXPLAIN ANALYZE
-----------
.
└── select
├── execution_time: 242.167μs
├── planning_time: 2.165637ms
├── total_time: 2.407804ms
└── field_iterators
├── labels
│ └── statement: SELECT sum(pointReq::integer) FROM "_internal"."monitor"."write" GROUP BY hostname
└── expression
├── labels
│ └── expr: sum(pointReq::integer)
├── create_iterator
│ ├── labels
│ │ ├── measurement: write
│ │ └── shard_id: 57
│ ├── cursors_ref: 1
│ ├── cursors_aux: 0
│ ├── cursors_cond: 0
│ ├── float_blocks_decoded: 0
│ ├── float_blocks_size_bytes: 0
│ ├── integer_blocks_decoded: 1
│ ├── integer_blocks_size_bytes: 931
│ ├── unsigned_blocks_decoded: 0
│ ├── unsigned_blocks_size_bytes: 0
│ ├── string_blocks_decoded: 0
│ ├── string_blocks_size_bytes: 0
│ ├── boolean_blocks_decoded: 0
│ ├── boolean_blocks_size_bytes: 0
│ └── planning_time: 1.401099ms
└── create_iterator
├── labels
│ ├── measurement: write
│ └── shard_id: 58
├── cursors_ref: 1
├── cursors_aux: 0
├── cursors_cond: 0
├── float_blocks_decoded: 0
├── float_blocks_size_bytes: 0
├── integer_blocks_decoded: 0
├── integer_blocks_size_bytes: 0
├── unsigned_blocks_decoded: 0
├── unsigned_blocks_size_bytes: 0
├── string_blocks_decoded: 0
├── string_blocks_size_bytes: 0
├── boolean_blocks_decoded: 0
├── boolean_blocks_size_bytes: 0
└── planning_time: 76.192μs
KILL QUERY
中斷當前正在運行的查詢。
kill_query_statement = "KILL QUERY" query_id .
其中,query_id是查詢ID,在SHOW QUERIES輸出中顯示為”qid”。
示例:
-- kill query with qid of 36 on the local host
KILL QUERY 36
SELECT
select_stmt = "SELECT" fields from_clause [ into_clause ] [ where_clause ]
[ group_by_clause ] [ order_by_clause ] [ limit_clause ]
[ offset_clause ] [ slimit_clause ] [ soffset_clause ] [ timezone_clause ] .
示例:
從所有以”cpu”開頭的measurement中選擇數據,并將數據寫入相同的measurement但保留策略為”cpu_1h”中。
SELECT mean("value") INTO "cpu_1h".:MEASUREMENT FROM /cpu.*/
查詢measurement中的數據,并將結果按天進行分組(帶有時區)。
SELECT mean("value") FROM "cpu" GROUP BY region, time(1d) fill(0) tz('America/Chicago')
SHOW CARDINALITY
指用于估計或精確計算measurement、序列、tag key、tag value和field key的基數的一組命令。
SHOW CARDINALITY命令有兩種可用的版本:估計和精確。估計值使用草圖進行計算,對于所有基數大小來說,這是一個安全默認值。精確值是直接對TSM(Time-Structured Merge Tree)數據進行計數,但是,對于基數大的數據來說,運行成本很高。除非必須要使用,否則,請使用估計的方法。
當數據庫啟用Time Series Index(TSI)時,才支持對time
進行過濾。
請查看特定的SHOW CARDINALITY命令獲得更多信息:
SHOW FIELD KEY CARDINALITY
SHOW MEASUREMENT CARDINALITY
SHOW SERIES CARDINALITY
SHOW TAG KEY CARDINALITY
SHOW TAG VALUES CARDINALITY
SHOW CONTINUOUS QUERIES
show_continuous_queries_stmt = "SHOW CONTINUOUS QUERIES" .
示例:
-- show all continuous queries
SHOW CONTINUOUS QUERIES
SHOW DATABASES
show_databases_stmt = "SHOW DATABASES" .
示例:
-- show all databases
SHOW DATABASES
SHOW DIAGNOSTICS
顯示節點信息,例如構建信息、運行時間、主機名、服務器配置、內存使用情況和Go運行時診斷。
show_diagnostics_stmt = "SHOW DIAGNOSTICS"
SHOW FIELD KEY CARDINALITY
除非使用ON <database>
指定數據庫,否則估計或精確計算當前數據庫的field key集的基數。
ON <database>、FROM <sources>、WITH KEY = <key>、WHERE <condition>、GROUP BY <dimensions>和LIMIT/OFFSET子句是可選的。當使用這些查詢子句時,查詢將回退到精確計數(exact count)。當啟用Time Series Index(TSI)時,才支持對time進行過濾。不支持在WHERE子句中使用time。
show_field_key_cardinality_stmt = "SHOW FIELD KEY CARDINALITY" [ on_clause ] [ from_clause ] [ where_clause ] [ group_by_clause ] [ limit_clause ] [ offset_clause ]
show_field_key_exact_cardinality_stmt = "SHOW FIELD KEY EXACT CARDINALITY" [ on_clause ] [ from_clause ] [ where_clause ] [ group_by_clause ] [ limit_clause ] [ offset_clause ]
示例:
-- show estimated cardinality of the field key set of current database
SHOW FIELD KEY CARDINALITY
-- show exact cardinality on field key set of specified database
SHOW FIELD KEY EXACT CARDINALITY ON mydb
SHOW FIELD KEY
show_field_keys_stmt = "SHOW FIELD KEYS" [on_clause] [ from_clause ] .
示例:
-- show field keys and field value data types from all measurements
SHOW FIELD KEYS
-- show field keys and field value data types from specified measurement
SHOW FIELD KEYS FROM "cpu"
SHOW GRANTS
show_grants_stmt = "SHOW GRANTS FOR" user_name .
示例:
-- show grants for jdoe
SHOW GRANTS FOR "jdoe"
SHOW MEASUREMENT CARDINALITY
除非使用ON <database>
指定數據庫,否則估計或精確計算當前數據庫的measurement集的基數。
ON <database>、FROM <sources>、WITH KEY = <key>、WHERE <condition>、GROUP BY <dimensions>和LIMIT/OFFSET子句是可選的。當使用這些查詢子句時,查詢將回退到精確計數(exact count)。當啟用Time Series Index(TSI)時,才支持對time進行過濾。不支持在WHERE子句中使用time。
show_measurement_cardinality_stmt = "SHOW MEASUREMENT CARDINALITY" [ on_clause ] [ from_clause ] [ where_clause ] [ group_by_clause ] [ limit_clause ] [ offset_clause ]
show_measurement_exact_cardinality_stmt = "SHOW MEASUREMENT EXACT CARDINALITY" [ on_clause ] [ from_clause ] [ where_clause ] [ group_by_clause ] [ limit_clause ] [ offset_clause ]
示例:
-- show estimated cardinality of measurement set on current database
SHOW MEASUREMENT CARDINALITY
-- show exact cardinality of measurement set on specified database
SHOW MEASUREMENT EXACT CARDINALITY ON mydb
SHOW MEASUREMENTS
show_measurements_stmt = "SHOW MEASUREMENTS" [on_clause] [ with_measurement_clause ] [ where_clause ] [ limit_clause ] [ offset_clause ] .
示例:
-- show all measurements
SHOW MEASUREMENTS
-- show measurements where region tag = 'uswest' AND host tag = 'serverA'
SHOW MEASUREMENTS WHERE "region" = 'uswest' AND "host" = 'serverA'
-- show measurements that start with 'h2o'
SHOW MEASUREMENTS WITH MEASUREMENT =~ /h2o.*/
SHOW QUERIES
show_queries_stmt = "SHOW QUERIES" .
示例:
-- show all currently-running queries
SHOW QUERIES
SHOW RETENTION POLICIES
show_retention_policies_stmt = "SHOW RETENTION POLICIES" [on_clause] .
示例:
-- show all retention policies on a database
SHOW RETENTION POLICIES ON "mydb"
SHOW SERIES
show_series_stmt = "SHOW SERIES" [on_clause] [ from_clause ] [ where_clause ] [ limit_clause ] [ offset_clause ] .
示例:
SHOW SERIES FROM "telegraf"."autogen"."cpu" WHERE cpu = 'cpu8'
SHOW SERIES CARDINALITY
除非使用ON <database>
指定數據庫,否則估計或精確計算當前數據庫的序列的基數。
序列基數是影響內存(RAM)使用量的主要因素。
ON <database>、FROM <sources>、WITH KEY = <key>、WHERE <condition>、GROUP BY <dimensions>和LIMIT/OFFSET子句是可選的。當使用這些查詢子句時,查詢將回退到精確計數(exact count)。當啟用Time Series Index(TSI)時,才支持對time進行過濾。不支持在WHERE子句中使用time。
show_series_cardinality_stmt = "SHOW SERIES CARDINALITY" [ on_clause ] [ from_clause ] [ where_clause ] [ group_by_clause ] [ limit_clause ] [ offset_clause ]
show_series_exact_cardinality_stmt = "SHOW SERIES EXACT CARDINALITY" [ on_clause ] [ from_clause ] [ where_clause ] [ group_by_clause ] [ limit_clause ] [ offset_clause ]
示例:
-- show estimated cardinality of the series on current database
SHOW SERIES CARDINALITY
-- show estimated cardinality of the series on specified database
SHOW SERIES CARDINALITY ON mydb
-- show exact series cardinality
SHOW SERIES EXACT CARDINALITY
-- show series cardinality of the series on specified database
SHOW SERIES EXACT CARDINALITY ON mydb
SHOW SHARD GROUPS
show_shard_groups_stmt = "SHOW SHARD GROUPS" .
示例:
SHOW SHARD GROUPS
SHOW SHARD
show_shards_stmt = "SHOW SHARDS" .
示例:
SHOW SHARDS
SHOW STATS
返回一個TSDB For InfluxDB?節點和可用(以啟用)的組件的可用組件的詳細統計信息。
show_stats_stmt = "SHOW STATS [ FOR '<component>' | 'indexes' ]"
SHOW STATS
SHOW STATS
命令不會列出關于索引的內存使用量—請使用SHOW STATS FOR 'indexes'
命令。SHOW STATS
返回的統計信息存儲在內存中,并且在節點重啟時重新設置為0,但是,每10秒會觸發一次SHOW STATS
來填充數據庫_internal
。
SHOW STATS FOR <component>
該命令返回指定組件(\)的統計信息。
對于
runtime
組件,該命令使用Go runtime返回TSDB For InfluxDB?系統的內存使用量概要。
SHOW STATS FOR 'indexes'
該命令返回所有索引的內存使用量,這是一個估計值。
SHOW STATS
不會列出索引的內存使用量,因為這可能是一個很耗資源的操作。
示例:
> show stats
name: runtime
-------------
Alloc Frees HeapAlloc HeapIdle HeapInUse HeapObjects HeapReleased HeapSys Lookups Mallocs NumGC NumGoroutine PauseTotalNs Sys TotalAlloc
4136056 6684537 4136056 34586624 5816320 49412 0 40402944 110 6733949 83 44 36083006 46692600 439945704
name: graphite
tags: proto=tcp
batches_tx bytes_rx connections_active connections_handled points_rx points_tx
---------- -------- ------------------ ------------------- --------- ---------
159 3999750 0 1 158110 158110
SHOW SUBSCRIPTIONS
show_subscriptions_stmt = "SHOW SUBSCRIPTIONS" .
示例:
SHOW SUBSCRIPTIONS
SHOW TAG KEY CARDINALITY
除非使用ON <database>
指定數據庫,否則估計或精確計算當前數據庫的tag key集的基數。
ON <database>、FROM <sources>、WITH KEY = <key>、WHERE <condition>、GROUP BY <dimensions>和LIMIT/OFFSET子句是可選的。當使用這些查詢子句時,查詢將回退到精確計數(exact count)。當啟用Time Series Index(TSI)時,才支持對time進行過濾。不支持在WHERE子句中使用time。
show_tag_key_cardinality_stmt = "SHOW TAG KEY CARDINALITY" [ on_clause ] [ from_clause ] [ where_clause ] [ group_by_clause ] [ limit_clause ] [ offset_clause ]
show_tag_key_exact_cardinality_stmt = "SHOW TAG KEY EXACT CARDINALITY" [ on_clause ] [ from_clause ] [ where_clause ] [ group_by_clause ] [ limit_clause ] [ offset_clause ]
示例:
-- show estimated tag key cardinality
SHOW TAG KEY CARDINALITY
-- show exact tag key cardinality
SHOW TAG KEY EXACT CARDINALITY
SHOW TAG KEYS
show_tag_keys_stmt = "SHOW TAG KEYS" [on_clause] [ from_clause ] [ where_clause ]
[ limit_clause ] [ offset_clause ] .
示例:
-- show all tag keys
SHOW TAG KEYS
-- show all tag keys from the cpu measurement
SHOW TAG KEYS FROM "cpu"
-- show all tag keys from the cpu measurement where the region key = 'uswest'
SHOW TAG KEYS FROM "cpu" WHERE "region" = 'uswest'
-- show all tag keys where the host key = 'serverA'
SHOW TAG KEYS WHERE "host" = 'serverA'
SHOW TAG VALUES
show_tag_values_stmt = "SHOW TAG VALUES" [on_clause] [ from_clause ] with_tag_clause [ where_clause ]
[ limit_clause ] [ offset_clause ] .
示例:
-- show all tag values across all measurements for the region tag
SHOW TAG VALUES WITH KEY = "region"
-- show tag values from the cpu measurement for the region tag
SHOW TAG VALUES FROM "cpu" WITH KEY = "region"
-- show tag values across all measurements for all tag keys that do not include the letter c
SHOW TAG VALUES WITH KEY !~ /.*c.*/
-- show tag values from the cpu measurement for region & host tag keys where service = 'redis'
SHOW TAG VALUES FROM "cpu" WITH KEY IN ("region", "host") WHERE "service" = 'redis'
SHOW TAG VALUES CARDINALITY
除非使用ON <database>
指定數據庫,否則估計或精確計算當前數據庫的指定tag key對應的tag value的基數。
ON <database>、FROM <sources>、WITH KEY = <key>、WHERE <condition>、GROUP BY <dimensions>和LIMIT/OFFSET子句是可選的。當使用這些查詢子句時,查詢將回退到精確計數(exact count)。當啟用Time Series Index(TSI)時,才支持對time進行過濾。
show_tag_values_cardinality_stmt = "SHOW TAG VALUES CARDINALITY" [ on_clause ] [ from_clause ] [ where_clause ] [ group_by_clause ] [ limit_clause ] [ offset_clause ] with_key_clause
show_tag_values_exact_cardinality_stmt = "SHOW TAG VALUES EXACT CARDINALITY" [ on_clause ] [ from_clause ] [ where_clause ] [ group_by_clause ] [ limit_clause ] [ offset_clause ] with_key_clause
示例:
-- show estimated tag key values cardinality for a specified tag key
SHOW TAG VALUES CARDINALITY WITH KEY = "myTagKey"
-- show estimated tag key values cardinality for a specified tag key
SHOW TAG VALUES CARDINALITY WITH KEY = "myTagKey"
-- show exact tag key values cardinality for a specified tag key
SHOW TAG VALUES EXACT CARDINALITY WITH KEY = "myTagKey"
-- show exact tag key values cardinality for a specified tag key
SHOW TAG VALUES EXACT CARDINALITY WITH KEY = "myTagKey"
SHOW USERS
show_users_stmt = "SHOW USERS" .
示例:
-- show all users
SHOW USERS
子句
from_clause = "FROM" measurements .
group_by_clause = "GROUP BY" dimensions fill(fill_option).
into_clause = "INTO" ( measurement | back_ref ).
limit_clause = "LIMIT" int_lit .
offset_clause = "OFFSET" int_lit .
slimit_clause = "SLIMIT" int_lit .
soffset_clause = "SOFFSET" int_lit .
timezone_clause = tz(string_lit) .
on_clause = "ON" db_name .
order_by_clause = "ORDER BY" sort_fields .
to_clause = "TO" user_name .
where_clause = "WHERE" expr .
with_measurement_clause = "WITH MEASUREMENT" ( "=" measurement | "=~" regex_lit ) .
with_tag_clause = "WITH KEY" ( "=" tag_key | "!=" tag_key | "=~" regex_lit | "IN (" tag_keys ")" ) .
表達式
binary_op = "+" | "-" | "*" | "/" | "%" | "&" | "|" | "^" | "AND" |
"OR" | "=" | "!=" | "<>" | "<" | "<=" | ">" | ">=" .
expr = unary_expr { binary_op unary_expr } .
unary_expr = "(" expr ")" | var_ref | time_lit | string_lit | int_lit |
float_lit | bool_lit | duration_lit | regex_lit .
其它
alias = "AS" identifier .
back_ref = ( policy_name ".:MEASUREMENT" ) |
( db_name "." [ policy_name ] ".:MEASUREMENT" ) .
db_name = identifier .
dimension = expr .
dimensions = dimension { "," dimension } .
field_key = identifier .
field = expr [ alias ] .
fields = field { "," field } .
fill_option = "null" | "none" | "previous" | int_lit | float_lit . | "linear"
host = string_lit .
measurement = measurement_name |
( policy_name "." measurement_name ) |
( db_name "." [ policy_name ] "." measurement_name ) .
measurements = measurement { "," measurement } .
measurement_name = identifier | regex_lit .
password = string_lit .
policy_name = identifier .
privilege = "ALL" [ "PRIVILEGES" ] | "READ" | "WRITE" .
query_id = int_lit .
query_name = identifier .
retention_policy = identifier .
retention_policy_option = retention_policy_duration |
retention_policy_replication |
retention_policy_shard_group_duration |
"DEFAULT" .
retention_policy_duration = "DURATION" duration_lit .
retention_policy_replication = "REPLICATION" int_lit .
retention_policy_shard_group_duration = "SHARD DURATION" duration_lit .
retention_policy_name = "NAME" identifier .
series_id = int_lit .
shard_id = int_lit .
sort_field = field_key [ ASC | DESC ] .
sort_fields = sort_field { "," sort_field } .
subscription_name = identifier .
tag_key = identifier .
tag_keys = tag_key { "," tag_key } .
user_name = identifier .
var_ref = measurement .
注釋
在InfluxQL語句中使用注釋來描述您的查詢。
單行注釋以兩個連字符(
--
)開頭,并且在TSDB For InfluxDB?檢測到換行符時結束。這種注釋方式不能跨越多行。多行注釋以
/*
開頭,并且以*/
結束。這種注釋方式可以跨越多行。多行注釋不支持嵌套的多行注釋。
查詢引擎內部
一旦您理解了語言本身,了解如何在查詢引擎中實現這些語言結構是十分重要的,因為這樣可以使您直觀地了解如何處理結果和如何創建有效的查詢。
一個查詢的生命周期如下所示:
符號化InfluxQL查詢語句,然后解析成一個抽象語法樹(abstract syntac tree,簡稱AST)。這是查詢本身的代碼表示。
將AST傳給
QueryExecutor
,它將查詢定向到合適的處理器(handler)。例如,與元數據相關的查詢由元數據服務執行,SELECT
語句由shard本身執行。然后,查詢引擎確定與
SELECT
語句中的時間范圍匹配的shard。在這些shard中,為語句中的每個field創建迭代器(iterator)。將迭代器傳到發射器(emitter),發射器將它們排出并連接結果中的數據點。發射器的工作是將簡單的time/value數據點轉換為更復雜的結果返回給客戶端。
理解迭代器
迭代器是查詢引擎的核心。迭代器提供一個簡單的接口,用于循環遍歷一組數據點。例如,這是浮點數上的迭代器:
type FloatIterator interface {
Next() *FloatPoint
}
通過接口IteratorCreator
創建迭代器:
type IteratorCreator interface {
CreateIterator(opt *IteratorOptions) (Iterator, error)
}
IteratorOptions
提供關于field選擇、時間范圍和維度的參數,使得迭代器創建者在規劃迭代器的時候可以使用這些參數。接口IteratorCreator
可以在多個層面使用,例如Shards
、Shard
和Engine
。這允許在適當的時候執行優化,例如返回預計算的COUNT()
。
迭代器不僅僅用于從存儲中讀取原始數據,迭代器還可以組合使用,以便它們為輸入迭代器(input iterator)提供額外的功能。例如,迭代器DistinctIterator
可以為輸入迭代器計算每個時間窗口的不同的值,或者,迭代器FillIterator
可以生成輸入迭代器中缺少的數據點。
這種組合也很適合用于聚合。例如,以下語句:
SELECT MEAN(value) FROM cpu GROUP BY time(10m)
在這種情況下,MEAN(value)
是一個迭代器MeanIterator
,它從底層的shard中包裝一個迭代器。然而,我們可以添加一個額外的迭代器來決定這些平均值的導數:
SELECT DERIVATIVE(MEAN(value), 20m) FROM cpu GROUP BY time(10m)
理解輔助field
因為InfluxQL允許用戶使用selector函數,例如,FIRST()
、LAST()
、MIN()
和MAX()
,所以查詢引擎必須提供一種與選定數據點同時返回相關數據的方法。
例如,以下查詢:
SELECT FIRST(value), host FROM cpu GROUP BY time(1h)
我們查詢每小時發生的第一個value
,同時我們也要獲得該數據點對應的host
。因為效率問題,Point
類型只指定了一個value
類型,所以我們將host
推送到該數據點的輔助field。這些輔助field將附加到數據點,直到它被傳送到發射器(在那里field會被拆分到它們自己的迭代器)。
內置迭代器
有許多輔助迭代器(helper iterators)可以讓我們構建查詢:
Merge Iterator(合并迭代器):該迭代器將一個或多個迭代器合并成一個有相同類型的新的迭代器。該迭代器保證在開始下一個窗口之前輸出當前窗口內的所有數據點,但是并不保證窗口內的數據點已經排好序,這使得不需要更高排序要求的聚合查詢能夠快速訪問。
Sorted Merge Iterator(排序合并迭代器):該迭代器也將一個或多個迭代器合并成一個有相同類型的新的迭代器。但是,該迭代器保證每個數據點都是按時間排好序的。這使得它的速度比
MergeIterator
慢,但是對于返回原始數據點的非聚合查詢,這種排序保證是必須的。Limit Iterator(限制迭代器):該迭代器限制了每個name/tag組的數據點個數。這是
LIMIT
和OFFSET
語法的實現。Fill Iterator(填充迭代器):該迭代器用額外的數據點填充在輸入迭代器中缺失的數據點,它可以提供
null
數據點、與前一個值相同的數據點、或者有特定值的數據點。Buffered Iterator(緩沖迭代器):該迭代器提供將一個數據點”unread”(未讀)并且返回緩沖區的能力,以便下次讀取它。這被廣泛用于為窗口提供前瞻。
Reduce Iterator(reduce迭代器):該迭代器為窗口中的每一個數據點調用reduction函數。當窗口內的所有運算完成后,該窗口的所有數據點會被輸出。這用于簡單聚合函數,例如
COUNT()
。Reduce Slice Iterator(reduce slice迭代器):該迭代器首先收集窗口內的所有數據點,然后立刻將它們全部傳送到一個reduction函數。結果從迭代器返回。這用于聚合函數,例如
DERIVATIVE()
。Transform Iterator(轉換迭代器):該迭代器對輸入迭代器中的每個數據點調用轉換函數。這用于執行二進制表達式。
Dedupe Iterator(去重迭代器)該迭代器只輸出不同的數據點。因為該迭代器非常消耗資源,所以它只用于小查詢,例如元查詢語句(meta query statements)。
調用迭代器
InfluxQL中的函數調用分兩個級別(level)實現:shard級別和引擎級別。
為了提高效率,有些調用可以在多個層被包裝,例如,COUNT()
可以在shard級別執行,然后一個CountIterator
可以包裝多個CountIterator
來計算所有shard的個數。這些迭代器可以使用NewCallIterator()
來創建。
有些迭代器更復雜或者需要在更高的級別實現。例如,DERIVATIVE()
首先需要獲得窗口內的所有數據點,然后再執行計算,該迭代器由引擎本身創建,并且永遠不會由更低級別創建。
InfluxDB? is a trademark registered by InfluxData, which is not affiliated with, and does not endorse, TSDB for InfluxDB?.