Write SQL queries
Write your own SQL queries against the evalink analytics data warehouse. You need Analytics Professional. See What you need.
Where you write SQL
- In a dashboard panel: click Open Analytics on the Analytics page and edit a panel. See Analytics dashboards.
- In a scheduled query: see Run a query on a schedule.
- Insights writes the SQL for you. To see it, click Details under the answer.
Rules for every query
- Write every table with the
analytics.prefix, for exampleFROM analytics.alarm. Without the prefix, the query may read an old table. See Moving from evalink analytics V1. - Times are numbers: milliseconds since 1 January 1970, UTC. To turn them into dates or filter by "today", use the time functions.
- Always limit alarms, workflows and the event log by time. Without a time range, the query reads the whole history and may time out.
- Yes/no columns hold the text
'1'(yes) and'0'(no), not true and false. - To count real alarms, filter with
alarm_value = '1'(an alarm, not a restore) andtest_message = '0'.
Main tables
| Table | What it holds |
|---|---|
alarm |
All alarms, that is the events sent by alarm systems. |
alarm_history_item |
Each step in an alarm's life with its time: created, shown, acknowledged, closed and so on. |
workflow |
Each case of handling alarms, by an operator or automatically. |
workflow_history_item |
Each change of a workflow: created, assigned, status changed and so on. |
workflow_step_completion |
The result of each workflow step. |
attached_alarm |
The alarms attached to a workflow. |
workflow_analyzer |
One row per executed workflow step, with the workflow's columns. Easiest way to analyse step results. |
event_log |
Everything that happened on a site: alarms, workflows, changes. Same as a site's event log. |
device |
Your sites. |
device_group |
Your site groups. |
virtual_device |
Your alarm systems, panels and receivers. |
user |
Your users. |
event_definition |
Event codes with their names, also translated. |
The big tables come in several versions with the same content. Pick the one that matches how you filter:
| Version | Use it when you filter by |
|---|---|
alarm, workflow, event_log |
Time |
alarm_by_id, workflow_by_id, workflow_analyzer_by_id |
ID |
alarm_by_device, workflow_by_device, event_log_by_device |
Site and time |
alarm_live, workflow_live, event_log_live |
The last seconds. alarm_live and workflow_live are about 15–30 seconds behind, event_log_live about 60–90 seconds. They lack some calculated columns. |
The full list of tables and columns is in Insights: ask it, for example, "Which columns does the alarm table have?"
Query performance rules
If a query is slow or times out, follow these best practices.
The warehouse has no indexes. It finds rows by the columns the table is sorted by. In tables that must be filtered by time, the first column is time.
- Filter by the sort columns. Use the table version that matches your filter, see Main tables.
- Select only the columns you need. Each column is read separately, so twice the columns take about twice as long.
- Use BETWEEN with both ends.
a.time BETWEEN time.start_of('day') AND time.now()is much faster thana.time > time.start_of('day'). - Don't rely on LIMIT. The query is computed in full before LIMIT cuts it. Use a time range instead:
-- Slow: sorts the whole table to find the latest alarm
SELECT * FROM analytics.alarm a ORDER BY a.time DESC LIMIT 1;
-- Fast: sorts only today's alarms
SELECT * FROM analytics.alarm a
WHERE a.time BETWEEN time.start_of('day') AND time.now()
ORDER BY a.time DESC LIMIT 1;
- Put the time filter inside a WITH block. Otherwise the block may be computed for the whole table first:
WITH recent_alarms AS (
SELECT a.alarm_id, a.alarm_code, a.time
FROM analytics.alarm a
WHERE time.last(a.time, '3 days')
)
SELECT alarm_code, time.ts(time, 'Europe/Madrid')
FROM recent_alarms;
- Join on the ID and
time. See Join tables.
Join tables
Join on the ID and time. Every table has its ID also as a <table>_id column and its time as time, so USING (time, <table>_id) works:
SELECT a.alarm_id, a.alarm_code, i.type
FROM analytics.alarm a
JOIN analytics.alarm_history_item i USING (time, alarm_id)
WHERE time.last(a.time, '1 week');
A join on the ID alone makes the warehouse look through the whole joined table.
Read JSON fields
Columns that hold JSON have a twin ending in _json, for example headers_json. Read a field with a dot and cast it:
SELECT a.alarm_code, a.headers_json."SITE_NO"::varchar
FROM analytics.alarm a
WHERE time.last(a.time, '1 week');
-- Nested fields work too: the German name of an event code
SELECT name_json.de.name
FROM analytics.event_definition
WHERE event_code = 'E110';
Names with capitals or special characters go in double quotes.
Run a query on a schedule
A scheduled query runs on its own and keeps every result, so you can chart it over time in a dashboard.
-
Go to Company > Analytics > SQL queries.
-
Click Add query.
-
Enter a Name. It must be unique in your company.
-
In SQL Statement, write the query in this form:
SELECT COUNT(*) AS value, d.name AS attribute FROM ...The first column is the result and must be named
value. The optional second column says what the result is about, for example the site name, and must be namedattribute. Neither expression may contain spaces. Each is cut at 255 characters. -
In Interval (sec), enter how often the query runs. The minimum is 60 seconds.
-
Leave Enable query on. The query starts to run once it has been checked.
-
Click Submit.
Each run is stored with its time in the custom_query_snapshot table. Use that table in a dashboard panel to chart the results.
In the list, a red warning in Validity means the last run failed. Hover over it to see the error.
Limits
| Limit | Value |
|---|---|
| Time a question to Insights may run | 270 seconds |
| Rows Insights reads | 100,000 |
| Rows Insights shows | 5,000 |
| Shortest interval of a scheduled query | 60 seconds |
If a query times out, see Query performance rules. If it hits the row limit, group the results in the query.
Moving from evalink analytics V1
If your company used evalink analytics before, your existing queries keep working: without the analytics. prefix, they still read the V1 tables, which keep their old columns. The new tables and columns are only in the analytics schema.
To use them, write analytics. before each table. When all your queries work with analytics., ask support to make analytics your default schema.