Skip to content

4.15 Database Query (SQL)

This section explains DB queries (SQL).

4.15.1 DB Query Overview#

When you start the DB query service, you can query the time series database using Structured Query Language (SQL) via Web-API (HTTP).

The syntax is unique to SpeeDBee Synapse, but is similar to SQL, which is used for common RDBs.

4.15.2 DB Query Detail#

This section explains the Web-API specification and syntax.

4.15.2.1 Web-API Specification#

For the Web-API specification of the DB query service, see here.

4.15.2.2 Syntax Specifications#

4.15.2.2.1 SELECT#

The SELECT clause is used to reference data registered in the time series database.

  1. Example statement: Retrieve five cpu_usage records of resource collector r1 for a range up to the current time
    SELECT "r1:cpu_usage" FROM SPDB WHERE _ts < NOW LIMIT 5;
    
  2. Example statement: Retrieve the cpu_usage data of resource collector r1 for a range up to the current time where cpu_usage is 10 or more
    SELECT "r1:cpu_usage" FROM SPDB WHERE _TS < NOW AND "r1:cpu_usage" >= 10
    
  3. Example statement: Retrieve the max and min values of dataA of PLC collector plc1 with an analysis time width of 5 seconds
    SELECT MAX("plc1:dataA"),MIN("plc1:dataA") FROM SPDB WHERE _ts < NOW GROUP BY TUMBLING(5,sec) LIMIT 10;
    

Syntax Specification

SELECT <select_list> FROM <table> [WHERE <condition>] [GROUP BY <window_condition> [HAVING <condition>]] [ORDER BY _TS [ASC/DESC]] [LIMIT <number> [OFFSET <start>]|LIMIT <start>,<row_count>]

Reserved words are not case sensitive.

  • <select_list>

Specify the column to extract or expression. You can specify multiple values separated by commas. Note that the column name must be enclosed in double quotation marks (") when specifying a column used in SpeeDBee Synapse. Column names enclosed in double quotation marks are case sensitive.

Example 1: Column name

"plcA:data1", "computeB:DATAa"

Example 2: Integer, floating decimal, string

3, -5, 1.52, 0.32e2, 'TEST-STR'
Integers in SQL are treated as int64. Floating decimals are treated as double and can be represented in exponential notation. Enclose strings in single quotation marks. Double quotation marks are treated as identifiers for column names, etc.

Example 3: Expression

"plcA:data2" + 3

Available operators Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder

Example 4: Function

MAX("compute1:dataA"), AVG("compute2:dataB")

Available mathematical functions Return value Description
ABS INT64 Absolute value (integer)
ACOS DOUBLE Arc cosine
ASIN DOUBLE Arc sine
ATAN DOUBLE Arc tangent
CEIL DOUBLE Round up after the decimal point
COS DOUBLE Cosine
COSH DOUBLE Hyperbolic cosine
EXP DOUBLE Exponential
FABS DOUBLE Absolute value (float)
FLOOR DOUBLE Round down after the decimal point
FMOD DOUBLE Surplus
LOG DOUBLE Natural logarithms
LOG10 DOUBLE Logarithm with base 10
POW DOUBLE Power
ROUND DOUBLE Round to after the decimal point
SIN DOUBLE Sine
SINH DOUBLE Hyperbolic sine
SQRT DOUBLE Square roots
TAN DOUBLE Tangent
TANH DOUBLE Hyperbolic tangent
Available aggregation and statistical functions Return value Description
AVEDEV DOUBLE mdev
AVG DOUBLE mean
COUNT INT64 count
CV DOUBLE cv
DOF INT64 degree of freedom
MAX DOUBLE max
MIN DOUBLE min
SE DOUBLE stder
STDEV DOUBLE ustdev (unbiased standard deviation)
STDEVP DOUBLE stdev (standard deviation)
SUM DOUBLE sum
SUMSQ DOUBLE sumsq (sum of squares)
VAR DOUBLE uvar
VARP DOUBLE sample var
Available representative values Return value Description
LAST Data type of the value Window last value
TOP Data type of the value Window top value

Example 5: Constant

M_PI

Available constants Return value Description
M_E DOUBLE Napier number, base of natural logarithm (e)
M_LOG2E DOUBLE log_e(e)
M_LOG10E DOUBLE log_10(e)
M_LN2 DOUBLE log_e(2)
M_LN10 DOUBLE log_e(10)
M_PI DOUBLE Pi
M_PI_2 DOUBLE Pi/2
M_PI_4 DOUBLE Pi/4
M_1_PI DOUBLE 1/Pi
M_2_SQRTPI DOUBLE 2/Pi
M_SQRT2 DOUBLE sqrt(2)
M_SQRT1_2 DOUBLE 1/sqrt(2)

Example 6: Pseudocolumn

_WID, _TS

Available pseudocolumns Description
_WID Window ID (ID that changes in analysis width units when GROUP BY is used)
_TS Timestamp (nanoseconds)
  • <table>

Specify what you want to search for time series data.

To search both memory and files

SPDB
To search memory only
SPDB.MEM
To search files only
SPDB.FILE

  • <condition>

Specify a conditional expression.

Example 1: Memory usage is less than 50 or CPU usage is 30 or less

"r1:mem_usage" < 50 OR "r1:cpu_usage" <= 30

Available operators Description
= Equivalent
!= Not match
<> Not match
< Less than
> More than
<= Less than or equal to
>= More than or equal to
IS [NOT] NULL NULL, not NULL
BETWEEN AND In range
BETWEEN EXCLUSIVE AND EXCLUSIVE Do not include the specified range
Available logical operators Description
AND Logical conjunction
OR Logical disjunction
NOT Negation
Operators available for _TS Description
_TS = 100000000 Equal to specified nanoseconds
_TS > 100000000 Exceeding specified nanoseconds
_TS >= 100000000 More than or equal to specified nanoseconds
_TS < 100000000 Less than specified nanoseconds
_TS <= 100000000 Less than or equal to specified nanoseconds
_TS BETWEEN 1000000000 AND 9000000000 In the specified nanosecond range
_TS BETWEEN 1000000000 EXCLUSIVE AND 9000000000 EXCLUSIVE Range excluding specified nanoseconds
_TS > '20230808T01:23:45.111222333' Comparison by string specification in ISO8601 format
_TS < NOW Comparison with the current time
_TS < NOW + 1 hour Set the current time + 1 hour as the range
_TS < NOW + 10 minutes Set the current time + 10 minutes as the range
_TS < NOW + 5 sec Set the current time + 5 seconds as the range
_TS < NOW - 100 msec Set the current time - 100 milliseconds as the range
_TS < NOW - 100 usec Set the current time - 100 microseconds as the range
_TS < NOW - 100 nsec Set the current time - 100 nanoseconds as the range

Note: _TS can only be used on the left side.

You cannot specify more than one time condition in the same SELECT clause.

Use AND to connect with other conditions. OR connection is not allowed.

  • <window_condition>

Specify the condition for window analysis. There are two types of windows: the tumbling window, which separates windows to be analyzed by time width, and the value change window, which separates windows by value change.

Example 1: Tumbling window (Split analysis window in 5 seconds)

GROUP BY TUMBLING(5,sec)

Example 2: Value change window (Separate analysis window when plcA:pressure changes)

GROUP BY VCHG("plcA:pressure")

Example 3: Value change window (Separate analysis window when plcA:pressure changes or after 30 seconds)

GROUP BY VCHG("plcA:pressure", 30, sec)

Example 4: Tumbling window + HAVING (Split analysis window in 5 seconds, and extract records whose plcA:pressure max is 10 or more)

GROUP BY TUMBLING(10,sec) HAVING MAX("plcA:pressure") >= 10

Example 5: Value change window + HAVING (Separate analysis window when plcA:pressure changes or after 30 seconds, and extract records whose plcA:pressure min is less than 5)

GROUP BY VCHG("plcA:pressure", 30, sec) HAVING MIN("plcA:pressure") < 5

  • <row_count>

Specify the number of output rows as an integer.

To set the number of data records to be retrieved to 3

LIMIT 3

  • <start>

Specify the number of rows to skip for the start position as an integer.

To retrieve the first three records after the fifth record

LIMIT 5,3

  • About ORDER BY

You can specify only _TS for ORDER BY. If ORDER BY is not specified, then it is considered that ascending order retrieval with ORDER BY _TS ASC is specified.

Descending order specification

ORDER BY _TS DESC

  • About specifying array type columns

Example 1: If com1:a is an array type, all array elements are referenced.

SELECT "com1:a" FROM SPDB;

Example 2: If com1:a is an array type, the first and second elements are referenced.
An array element is also specified in the conditional expression.

SELECT "com1:a"[0], "com1:a"[1] FROM SPDB WHERE "com1:a"[0] > 1;

Example 3: If com1:a is an array type, an array is specified in the conditional expression.
This is implicitly equivalent to com1:a[0].

SELECT "com1:a"[0], "com1:a"[1] FROM SPDB WHERE "com1:a" > 1;

Example 4: This is an example of an array search ANY. Assume that com1:a has two elements. The following two example statements are equivalent.
ANY specifies that all elements are compared and true is returned if any of them is true.

SELECT "com1:a"[0], "com1:a"[1] FROM SPDB WHERE 1 < ANY("com1:a");
SELECT "com1:a"[0], "com1:a"[1] FROM SPDB WHERE 1 < "com1:a"[0] OR 1 < "com1:a"[1];

Note: ANY can only be used on the right side.

Example 5: This is an example of array search ALL. Assume that com1:a has two elements. The following two example statements are equivalent.
ALL specifies that all elements are compared and true is returned if all of them are true.

SELECT "com1:a"[0], "com1:a"[1] FROM SPDB WHERE 1 < ALL("com1:a");
SELECT "com1:a"[0], "com1:a"[1] FROM SPDB WHERE 1 < "com1:a"[0] AND 1 < "com1:a"[1];

Note: ALL can only be used on the right side.

4.15.2.2.2 SHOW#

The SHOW clause is used to reference meta information in the time series database. Currently, only alias names (which correspond to the column names created by SpeeDBee Synapse) can be referenced.
If ORDER BY is omitted, no sort order is guaranteed.

  • SHOW ALIAS

Example 1: Retrieve a list of alias names (ascending)

SHOW ALIAS ORDER BY ALIAS_NAME ASC

Example 2: Retrieve a list of alias names (descending)

SHOW ALIAS ORDER BY ALIAS_NAME DESC

Syntax Specification

SHOW ALIAS [ORDER BY ALIAS_NAME [ASC/DESC]]

4.15.2.2.3 SET#

This syntax is used to assign a value to a global variable. In SQL, global variables must always be prefixed with $.

Example 1: Assigning an integer

SET $GVAR0 TO 10;

Example 2: Assigning a negative value

SET $PVAR0 -111;

Example 3: Assigning a floating decimal

SET $GVAR1 1.235;

Example 4: Assigning in exponential expression

SET $PVAR1 -1.234E-2

Example 5: Assigning a string

SET $PVAR2 'abcd'

Example of referencing the assigned global variables

SELECT \"com1:a\",$GVAR1, $PVAR2 FROM SPDB WHERE _ts BETWEEN NOW - 1 sec AND NOW ORDER BY _TS DESC LIMIT 1;

Syntax Specification

SET global variable name [TO] literal value

4.15.2.2.4 INSERT#

The INSERT clause is used to register data into the time series database. Data can be registered into columns of existing components. You can use SHOW to retrieve a list of column names into which you can register data.
The table specified immediately after [INTO] in the INSERT statement is fixed to SPDB.

Example 1: Insert the value "2" into the column "cpu_usage" of the resource collector "resource1 (resource collector)"

INSERT INTO SPDB (\"resource1:cpu_usage\") VALUES (2);
Note: If the timestamp is omitted, the system time of Operating machine is used as the timestamp to be registered.

Example 2: Insert the value "10" into the column "cpu_usage" of the resource collector "resource1 (resource collector)" with the specified time "April 3, 2025 13:02:09"

INSERT INTO SPDB (_TS, \"resource1:cpu_usage\") VALUES ('2025-04-03T13:02:09', 10);
INSERT INTO SPDB (_TS, \"resource1:cpu_usage\") VALUES (1743685329000000000, 10);

Example 3: Insert the integer value "10"

INSERT INTO SPDB (\"customcomp1:columnA\") VALUES (10);

Example 4: Insert the floating decimal value "3.14"

INSERT INTO SPDB (\"customcomp1:columnB\") VALUES (3.14);

Example 5: Insert the string value "ABCDE"

INSERT INTO SPDB (\"customcomp1:columnC\") VALUES ('ABCDE');

Example 6: Insert the array values {3,4}

INSERT INTO SPDB (\"customcomp1:columnD\") VALUES ({3,4});

Example 7: Insert multiple data into multiple columns of one collector

INSERT INTO SPDB (\"customcomp1:columnA\", \"customcomp1:columnB\",\"customcomp1:columnC\",\"customcomp1:columnD\",\"customcomp1:columnE\") VALUES (10,3.14,'ABCDE',{3,4},'00000000000000000000000000')

Example 8: Insert multiple data into multiple columns of multiple collectors

INSERT INTO SPDB (\"resource1:cpu_usage\", \"customcomp1:columnA\", \"customcomp1:columnB\",\"customcomp1:columnC\",\"customcomp1:columnD\",\"customcomp1:columnE\") VALUES (99,10,3.14,'ABCDE',{3,4},'00000000000000000000000000');

Syntax Specification

INSERT [INTO] SPDB (_TS, <column_name>,...) VALUES (timestamp value, value, ...) 

4.15.3 Cautions in using the DB query service#

The following provides cautions in using the DB query service.

The DB query service of SpeeDBee Synapse, in its design, aims to maintain stable operation even under heavy load. However, the execution of DB queries is highly dependent on the system load condition.
In particular, if the system is running under heavy load, DB query response performance may be temporarily degraded.

System load from query condition

The broader the scope of data that you specify in a query to the database, the heavier the load on the system is.
For example, a query covering a long period or a query with few narrowing-down conditions must scan a large amount of data, which not only increases response time but can affect other processing.

For stable system operation, note the following points when executing queries.

Notes on query execution

  • Set an access token
    By publishing an access token and applying it to the DB query service, you can restrict the users who can use DB queries.

  • Execute queries to the minimum extent necessary
    Narrow down to the required data and set the period and conditions as specifically as possible.

  • Avoid high load times
    If possible, you should avoid executing queries during periods of heavy system activity.