| layout | default |
|---|---|
| title | SQL Cookbook |
| description | 面向 SonnetDB 当前真实能力的常用 SQL 模板与场景化示例。 |
| permalink | /sql-cookbook/ |
这份 cookbook 面向“直接复制一段能跑的 SQL”的场景,补充 [SQL 参考]({{ '/sql-reference/' | relative_url }}) 中偏规则化的说明。
示例以当前仓库 demo.sql 和已有回归测试覆盖的语法为准,只使用 SonnetDB 当前版本已经实现的写法。
- 时间列固定叫
time,表示 Unix 毫秒时间戳。 - 时序数据建模用
CREATE MEASUREMENT;关系表元数据、小对象等可用CREATE TABLE ... PRIMARY KEY (...)。 - Measurement 聚合可按 TAG 和
time(...)分组。 - 跨模型语法以 SQL 参考中的对应入口为准。例如 measurement 与关系表的混合 JOIN 支持内连接,连接键为 measurement 的 TAG。
- 想查“这个库里有什么”,时序数据先
SHOW MEASUREMENTS/DESCRIBE MEASUREMENT <name>,关系表先SHOW TABLES/DESCRIBE TABLE <name>。
适用于服务端模式的控制面 SQL。
CREATE DATABASE demo;
CREATE USER viewer WITH PASSWORD 'viewer123';
CREATE USER writer WITH PASSWORD 'writer456';
CREATE USER admin2 WITH PASSWORD 'admin789' SUPERUSER;
GRANT READ ON DATABASE demo TO viewer;
GRANT WRITE ON DATABASE demo TO writer;
GRANT ADMIN ON DATABASE demo TO admin2;常用检查语句:
SHOW DATABASES;
SHOW USERS;
SHOW GRANTS;
SHOW GRANTS FOR viewer;
SHOW TOKENS;
SHOW TOKENS FOR writer;CREATE MEASUREMENT cpu (
host TAG,
region TAG,
usage FIELD FLOAT NULL,
cores FIELD INT,
throttled FIELD BOOL,
label FIELD STRING NOT NULL
);CREATE MEASUREMENT documents (
source TAG,
category TAG,
title FIELD STRING,
score FIELD FLOAT,
embedding FIELD VECTOR(4)
);建表后常用自检:
SHOW MEASUREMENTS;
DESCRIBE MEASUREMENT cpu;
DESCRIBE MEASUREMENT documents;NULL / NOT NULL 可以写在列声明后,主要用于兼容常见 SQL 生成器;当前不会持久化为 catalog 约束,也不会改变写入行为。SonnetDB 的 field 是稀疏的:某个时间点没写入某个 field,查询时该列返回 NULL。
CREATE MEASUREMENT sensors (
device TAG NOT NULL,
temperature FIELD FLOAT NULL,
pressure FIELD FLOAT NOT NULL
);当前 DEFAULT 只作为保留语法被 parser 接受,执行 CREATE MEASUREMENT 会明确报不支持。需要默认值时,在应用侧或写入 SQL 中直接提供该值;需要缺值时,省略该 field。
INSERT INTO cpu (time, host, region, usage, cores, throttled, label)
VALUES (1713657600000, 'server-01', 'cn-hz', 0.42, 8, FALSE, 'normal');INSERT INTO cpu (time, host, region, usage, cores, throttled, label) VALUES
(1713657600000, 'server-01', 'cn-hz', 0.42, 8, FALSE, 'normal'),
(1713657660000, 'server-01', 'cn-hz', 0.55, 8, FALSE, 'normal'),
(1713657720000, 'server-01', 'cn-hz', 0.61, 8, FALSE, 'normal');INSERT INTO documents (time, source, category, title, score, embedding) VALUES
(1713657600000, 'wiki', 'tech', '时序数据库简介', 0.92, [0.10, 0.20, 0.30, 0.40]),
(1713657601000, 'blog', 'tech', 'SonnetDB 快速入门', 0.95, [0.11, 0.21, 0.29, 0.39]);SELECT *
FROM cpu
WHERE host = 'server-01'
AND region = 'cn-hz';SELECT time, host, usage
FROM cpu
WHERE host = 'server-01'
AND time >= 1713657600000
AND time < 1713658200000;SELECT
abs(usage - 0.5) AS deviation,
round(usage * 100, 1) AS usage_pct,
sqrt(cores) AS sqrt_cores,
log(cores, 2) AS log2_cores,
coalesce(label, 'unknown') AS safe_label
FROM cpu
WHERE host = 'server-01';SELECT c.time, c.host, c."usage"
FROM cpu AS c
WHERE c.host = 'server-01'
ORDER BY c.time ASC
LIMIT 10;measurement 单表查询支持别名限定列名;需要把设备、租户、站点等维度补到时序结果时,可使用 MM4 的 measurement JOIN 关系维表。
SELECT time, host, usage
FROM cpu
WHERE host = 'server-01'
ORDER BY time ASC
LIMIT 5 OFFSET 0;SELECT time, host, usage
FROM cpu
WHERE host = 'server-01'
ORDER BY time ASC
OFFSET 5 ROWS FETCH NEXT 5 ROWS ONLY;SELECT t.time, d.name, d.site, t.value
FROM temperature AS t
JOIN devices AS d ON t.device_id = d.id
WHERE d.tenant = 'tenant-1'
AND t.time >= 1713657600000
ORDER BY t.time DESC
LIMIT 100;当前 JOIN 第一版限定为一个 measurement 与一个关系表的 inner 等值 JOIN;measurement 侧连接键必须是 TAG 列。有歧义的列名请使用 alias.column。
SELECT
count(usage) AS cnt,
sum(usage) AS total,
min(usage) AS min_usage,
max(usage) AS max_usage,
avg(usage) AS avg_usage,
first(usage) AS first_usage,
last(usage) AS last_usage
FROM cpu
WHERE host = 'server-01';SELECT
stddev(usage) AS std,
variance(usage) AS var,
spread(usage) AS spread,
median(usage) AS median,
percentile(usage, 95) AS p95,
histogram(usage, 5) AS hist
FROM cpu
WHERE host = 'server-01';SELECT
avg(usage) AS avg_usage,
max(usage) AS max_usage,
count(usage) AS cnt
FROM cpu
WHERE host = 'server-01'
GROUP BY time(2m);说明:
- Measurement 的时间桶语法为
GROUP BY time(...),可以同时按 TAG 分组,例如GROUP BY host, time(2m)。 - 需要桶起始时间时,在投影中显式加入
time AS bucket_start。
对于含有 occurred_at DATETIME2 的关系表 events,可以用 DATE_TRUNC 截出每个月的起点,再对该时间值分组。
SELECT DATE_TRUNC('month', occurred_at) AS month_start, COUNT(*) AS event_count
FROM events
GROUP BY DATE_TRUNC('month', occurred_at)
ORDER BY month_start;DATE_TRUNC 保留 DATETIME2 的结果类型。DATETIMEOFFSET 按 UTC 截断并返回零偏移。
CREATE TABLE request_samples (id INT, site STRING, latency_ms DECIMAL(8,2), PRIMARY KEY (id));
INSERT INTO request_samples (id, site, latency_ms) VALUES
(1, 'A', 13.25), (2, 'A', 20.50), (3, 'B', 10.00), (4, 'B', NULL);
SELECT site, median(latency_ms) AS median_ms, p50(latency_ms) AS p50_ms,
p90(latency_ms) AS p90_ms, percentile(latency_ms, 95) AS p95_ms,
p95(latency_ms) AS fixed_p95_ms, p99(latency_ms) AS p99_ms,
stddev(latency_ms) AS sample_sd, variance(latency_ms) AS sample_variance
FROM request_samples
GROUP BY site
HAVING COUNT(latency_ms) > 0
ORDER BY p99_ms DESC;这些统计接受 INT、FLOAT 和 DECIMAL,返回可为 NULL 的 FLOAT。percentile 的第二个参数是 (0, 100] 内的数值字面量或绑定参数。TDigest 估算分位数,样本方差和标准差在少于两个有效值时返回 NULL。
SELECT site, spread(latency_ms) AS range_ms, mode(latency_ms) AS most_common_ms,
histogram(latency_ms, 10) AS buckets, distinct_count(latency_ms) AS distinct_values,
tdigest_agg(latency_ms) AS digest
FROM request_samples
GROUP BY site
ORDER BY distinct_values DESC;distinct_count 返回精确计数,histogram 和 tdigest_agg 返回 JSON 文本。SQL 聚合参考
SELECT time, difference(usage) AS diff_usage
FROM cpu
WHERE host = 'server-01';SELECT time, delta(usage) AS delta_usage
FROM cpu
WHERE host = 'server-01';SELECT time, derivative(usage) AS rate_per_sec
FROM cpu
WHERE host = 'server-01';SELECT time, non_negative_derivative(usage) AS nn_rate
FROM cpu
WHERE host = 'server-01';SELECT time, cumulative_sum(usage) AS cumsum
FROM cpu
WHERE host = 'server-01';SELECT time, moving_average(usage, 3) AS ma3
FROM cpu
WHERE host = 'server-01';SELECT time, ewma(usage, 0.3) AS ewma_usage
FROM cpu
WHERE host = 'server-01';SELECT time, state_changes(throttled) AS changed
FROM cpu
WHERE host = 'server-01';SELECT time, state_duration(throttled) AS duration_ms
FROM cpu
WHERE host = 'server-01';SELECT
time,
temperature,
pid_series(temperature, 75.0, 0.6, 0.1, 0.05) AS valve
FROM reactor
WHERE device = 'r1'
ORDER BY time ASC;SELECT
pid_estimate(temperature, 'imc', 1.0, 0.1, 0.1, NULL) AS tuning_json
FROM reactor
WHERE device = 'r1'
AND time >= 1713657600000
AND time < 1713657800000;SELECT *
FROM forecast(cpu, usage, 5, 'linear')
WHERE host = 'server-01';SELECT
time,
usage,
anomaly(usage, 'mad', 2.5) AS is_outlier
FROM cpu
WHERE host = 'server-01';SELECT
time,
value,
changepoint(value, 'cusum', 4.0) AS shift_detected
FROM signal
WHERE source = 's-1';SELECT *
FROM knn(documents, embedding, [0.10, 0.20, 0.30, 0.40], 3);SELECT *
FROM knn(documents, embedding, [0.10, 0.20, 0.30, 0.40], 3, 'l2');SELECT *
FROM knn(documents, embedding, [0.10, 0.20, 0.30, 0.40], 5, 'cosine')
WHERE source = 'wiki'
AND time >= 1713657600000
AND time < 1713657605000;SELECT
cosine_distance(embedding, [0.10, 0.20, 0.30, 0.40]) AS cos_dist,
l2_distance(embedding, [0.10, 0.20, 0.30, 0.40]) AS l2_dist,
inner_product(embedding, [0.10, 0.20, 0.30, 0.40]) AS dot_prod,
vector_norm(embedding) AS norm
FROM documents
WHERE source = 'wiki';SHOW MEASUREMENTS;
SHOW TABLES;DESCRIBE MEASUREMENT cpu;
DESCRIBE MEASUREMENT documents;
DESC reactor;DELETE FROM cpu
WHERE host = 'server-01'
AND time >= 1713658080000
AND time <= 1713658140000;DELETE FROM signal
WHERE source = 's-1';建议先用对应的 SELECT 估算影响范围,再执行 DELETE。
不要这样写:
CREATE TABLE cpu (...); -- 时序数据应改为 CREATE MEASUREMENT
SELECT usage, avg(usage) FROM cpu GROUP BY usage; -- measurement 的 FIELD 不可用作分组键
SELECT time_bucket(time, '1m'), avg(usage) ...; -- measurement 用 GROUP BY time(1m)
UPDATE cpu SET usage = 1.0 WHERE ...; -- measurement 普通数值 FIELD 不支持 UPDATE;已有 VECTOR FIELD 可按 TAG/time 条件更新如果你拿不准当前能力边界:
- 先看 [SQL 参考]({{ '/sql-reference/' | relative_url }})
- 再让 Copilot 先
SHOW MEASUREMENTS/DESCRIBE MEASUREMENT或SHOW TABLES/DESCRIBE TABLE - 写入或删除前,优先走只读模式先起草 SQL