SQL Query Guide

MynaDB uses Apache DataFusion for SQL queries, supporting standard SQL syntax including JOINs, aggregations, window functions, CTEs, and subqueries.

Quick Start

bash
# Simple query (short table name)
curl 'http://localhost:7000/query/mydb' -d 'SELECT * FROM cpu LIMIT 10'

# Fully qualified table name
curl 'http://localhost:7000/query/mydb' -d 'SELECT * FROM mydb.default.cpu LIMIT 10'

# GET request (for simple queries)
curl 'http://localhost:7000/query/mydb?sql=SELECT * FROM cpu LIMIT 10'

Table Naming

MynaDB uses catalog.schema.table structure:

Format Example Description
Short name cpu Auto-prefixed (recommended)
Schema.table default.cpu Explicit schema
Full path mydb.default.cpu Fully qualified

SELECT Statements

All Columns

SELECT * FROM cpu LIMIT 10

Specific Columns

SELECT host, usage, time FROM cpu

Column Aliases

SELECT host, usage AS cpu_usage, time AS ts FROM cpu

DISTINCT

SELECT DISTINCT host FROM cpu
SELECT COUNT(DISTINCT host) AS unique_hosts FROM cpu

Filtering (WHERE)

Comparison Operators

SELECT * FROM cpu WHERE usage > 80
SELECT * FROM cpu WHERE usage >= 50 AND usage <= 90
SELECT * FROM cpu WHERE host = 'server01'
SELECT * FROM cpu WHERE host != 'server01'

Logical Operators

SELECT * FROM cpu WHERE usage > 80 AND region = 'us-west'
SELECT * FROM cpu WHERE region = 'us-west' OR region = 'us-east'
SELECT * FROM cpu WHERE NOT (usage < 50)

IN / NOT IN

SELECT * FROM cpu WHERE host IN ('server01', 'server02', 'server03')
SELECT * FROM cpu WHERE region NOT IN ('eu-west')

BETWEEN

SELECT * FROM cpu WHERE usage BETWEEN 50 AND 80
SELECT * FROM cpu WHERE time BETWEEN 1703251234567000000 AND 1703251300000000000

LIKE Pattern Matching

SELECT * FROM cpu WHERE host LIKE 'server%'
SELECT * FROM cpu WHERE host LIKE '%01'
SELECT * FROM cpu WHERE host LIKE 'web_server_'

NULL Handling

SELECT * FROM cpu WHERE region IS NULL
SELECT * FROM cpu WHERE region IS NOT NULL

Aggregations

Basic Functions

SELECT COUNT(*) FROM cpu
SELECT AVG(usage) FROM cpu
SELECT SUM(usage) FROM cpu
SELECT MIN(usage), MAX(usage) FROM cpu

GROUP BY

SELECT host, AVG(usage) AS avg_cpu
FROM cpu
GROUP BY host

SELECT host, region, COUNT(*) AS samples
FROM cpu
GROUP BY host, region

HAVING

SELECT host, AVG(usage) AS avg_cpu
FROM cpu
GROUP BY host
HAVING AVG(usage) > 50

ORDER BY

SELECT * FROM cpu ORDER BY usage DESC
SELECT * FROM cpu ORDER BY time ASC
SELECT * FROM cpu ORDER BY host ASC, usage DESC

JOINs

INNER JOIN

SELECT c.host, c.usage AS cpu, m.used AS memory
FROM cpu c
JOIN memory m ON c.host = m.host

LEFT JOIN

SELECT c.host, c.usage, m.used
FROM cpu c
LEFT JOIN memory m ON c.host = m.host

Multiple JOINs

SELECT c.host, c.usage, m.used, d.free
FROM cpu c
LEFT JOIN memory m ON c.host = m.host
LEFT JOIN disk d ON c.host = d.host

Window Functions

ROW_NUMBER / RANK

SELECT host, usage,
  ROW_NUMBER() OVER (ORDER BY usage DESC) AS row_num,
  RANK() OVER (ORDER BY usage DESC) AS rnk
FROM cpu

Moving Average

SELECT time, usage,
  AVG(usage) OVER (
    ORDER BY time
    ROWS BETWEEN 2 PRECEDING AND CURRENT ROW
  ) AS moving_avg
FROM cpu

LAG / LEAD

SELECT time, usage,
  LAG(usage, 1) OVER (ORDER BY time) AS prev_usage,
  LEAD(usage, 1) OVER (ORDER BY time) AS next_usage
FROM cpu

Partition By

SELECT host, time, usage,
  AVG(usage) OVER (PARTITION BY host ORDER BY time) AS host_avg
FROM cpu

Advanced Queries

Common Table Expressions (CTE)

WITH host_stats AS (
  SELECT host, AVG(usage) AS avg_usage
  FROM cpu
  GROUP BY host
)
SELECT * FROM host_stats
WHERE avg_usage > 50

Subqueries

SELECT * FROM cpu
WHERE usage > (SELECT AVG(usage) FROM cpu)

CASE Statements

SELECT host, usage,
  CASE
    WHEN usage < 30 THEN 'low'
    WHEN usage < 70 THEN 'medium'
    ELSE 'high'
  END AS level
FROM cpu

UNION

SELECT host, 'cpu' AS metric, usage AS value FROM cpu
UNION ALL
SELECT host, 'memory' AS metric, used AS value FROM memory

Time Series Functions

New: MynaDB now supports InfluxDB-compatible time series functions including GROUP BY time(), first_value(), last_value(), and time_bucket().

GROUP BY time() - Time Bucketing

Group data into time intervals using InfluxDB-style syntax. The interval supports units: ns, u/us, ms, s, m, h, d, w.

-- 5-minute average CPU usage per host
SELECT host, AVG(usage) AS avg_cpu
FROM cpu
WHERE time >= 1704067200000000000
GROUP BY time(5m), host

-- Hourly request counts
SELECT COUNT(*) AS requests
FROM api_logs
WHERE time >= NOW() - INTERVAL '24 hours'
GROUP BY time(1h)

-- 30-second intervals with offset
SELECT AVG(temperature) AS avg_temp
FROM sensors
GROUP BY time(30s, 10s)  -- 30s buckets, 10s offset

time_bucket() Function

The underlying UDF for time bucketing. Use directly for more control:

-- time_bucket(time_col, interval_nanos, offset_nanos)
SELECT
  time_bucket(time, 300000000000, 0) AS bucket,  -- 5 min = 300B ns
  AVG(usage) AS avg_usage
FROM cpu
GROUP BY time_bucket(time, 300000000000, 0)
ORDER BY bucket

first_value() and last_value()

Get the first or last value in a time-ordered group. Essential for downsampling time series data.

-- first_value(value_column, time_column)
-- Returns the value at the minimum time in each group

-- last_value(value_column, time_column)
-- Returns the value at the maximum time in each group

-- Example: First and last CPU reading per 5-minute bucket
SELECT
  time_bucket(time, 300000000000, 0) AS bucket,
  host,
  first_value(usage, time) AS first_cpu,
  last_value(usage, time) AS last_cpu,
  AVG(usage) AS avg_cpu,
  MAX(usage) - MIN(usage) AS spread
FROM cpu
GROUP BY time_bucket(time, 300000000000, 0), host
ORDER BY bucket, host

Fill Options (Gap Filling)

Handle missing time buckets in results:

-- fill(null) - Default, missing buckets not included
SELECT AVG(usage) FROM cpu
WHERE time >= 1704067200000000000
GROUP BY time(5m)
fill(null)

-- fill(none) - Same as null, omit empty buckets
-- fill(previous) - Use previous bucket's value
-- fill(linear) - Linear interpolation between values
-- fill(0) or fill(value) - Use specific value

InfluxDB Function Mapping

InfluxDB Function MynaDB Equivalent Description
mean(field) AVG(field) Average value
sum(field) SUM(field) Sum of values
count(field) COUNT(field) Count of values
min(field) MIN(field) Minimum value
max(field) MAX(field) Maximum value
first(field) first_value(field, time) First value by time
last(field) last_value(field, time) Last value by time
spread(field) MAX(field) - MIN(field) Range of values
stddev(field) STDDEV(field) Standard deviation
median(field) approx_median(field) Approximate median

Complete Downsampling Example

Downsample high-frequency data to 1-hour intervals for long-term storage:

-- Create hourly summary from minute-level data
SELECT
  time_bucket(time, 3600000000000, 0) AS hour,
  host,
  COUNT(*) AS sample_count,
  AVG(cpu_usage) AS avg_cpu,
  MAX(cpu_usage) AS max_cpu,
  MIN(cpu_usage) AS min_cpu,
  first_value(cpu_usage, time) AS first_cpu,
  last_value(cpu_usage, time) AS last_cpu,
  AVG(memory_used) AS avg_memory,
  MAX(disk_io) AS peak_disk_io
FROM system_metrics
WHERE time >= NOW() - INTERVAL '7 days'
GROUP BY time_bucket(time, 3600000000000, 0), host
ORDER BY hour DESC, host

Tips