SQL 查询示例
常见时序操作的即用型 SQL 示例。为清晰起见,所有示例使用 POST 请求,
但对于简单查询也可以使用带 ?sql= 参数的 GET 请求。
💡 提示
使用短表名(例如 cpu)代替完全限定名。系统会自动添加 catalog 和 schema 前缀。
基础查询
查询所有数据
curl 'http://localhost:7000/query/mydb' -d 'SELECT * FROM cpu LIMIT 10'
选择特定列
curl 'http://localhost:7000/query/mydb' -d 'SELECT host, usage, time FROM cpu'
排序
# 使用量最高的在前
curl 'http://localhost:7000/query/mydb' -d 'SELECT * FROM cpu ORDER BY usage DESC LIMIT 10'
# 最新记录在前
curl 'http://localhost:7000/query/mydb' -d 'SELECT * FROM cpu ORDER BY time DESC LIMIT 10'
去重值
# 唯一主机
curl 'http://localhost:7000/query/mydb' -d 'SELECT DISTINCT host FROM cpu'
# 统计唯一主机数
curl 'http://localhost:7000/query/mydb' -d 'SELECT COUNT(DISTINCT host) as unique_hosts FROM cpu'
聚合
基础聚合
# 计数
curl 'http://localhost:7000/query/mydb' -d 'SELECT COUNT(*) as total FROM cpu'
# 平均值
curl 'http://localhost:7000/query/mydb' -d 'SELECT AVG(usage) as avg_usage FROM cpu'
# 最小值 / 最大值
curl 'http://localhost:7000/query/mydb' -d 'SELECT MIN(usage) as min, MAX(usage) as max FROM cpu'
# 求和
curl 'http://localhost:7000/query/mydb' -d 'SELECT SUM(usage) as total_usage FROM cpu'
GROUP BY
# 按主机分组求平均
curl 'http://localhost:7000/query/mydb' -d 'SELECT host, AVG(usage) as avg_cpu FROM cpu GROUP BY host'
# 按主机分组的多个聚合
curl 'http://localhost:7000/query/mydb' -d "
SELECT
host,
COUNT(*) as count,
AVG(usage) as avg,
MIN(usage) as min,
MAX(usage) as max
FROM cpu
GROUP BY host
"
HAVING
# 平均使用率 > 50% 的主机
curl 'http://localhost:7000/query/mydb' -d "
SELECT host, AVG(usage) as avg_cpu
FROM cpu
GROUP BY host
HAVING AVG(usage) > 50
"
百分位数
curl 'http://localhost:7000/query/mydb' -d "
SELECT
approx_percentile_cont(usage, 0.50) as p50,
approx_percentile_cont(usage, 0.95) as p95,
approx_percentile_cont(usage, 0.99) as p99
FROM cpu
"
过滤
WHERE 子句
# 按数值过滤
curl 'http://localhost:7000/query/mydb' -d 'SELECT * FROM cpu WHERE usage > 80'
# 按字符串过滤
curl 'http://localhost:7000/query/mydb' -d "SELECT * FROM cpu WHERE host = 'server01'"
# 多条件(AND)
curl 'http://localhost:7000/query/mydb' -d "
SELECT * FROM cpu
WHERE usage > 50 AND region = 'us-west'
"
# 多条件(OR)
curl 'http://localhost:7000/query/mydb' -d "
SELECT * FROM cpu
WHERE region = 'us-west' OR region = 'us-east'
"
时间范围查询
# 指定时间之后
curl 'http://localhost:7000/query/mydb' -d "
SELECT * FROM cpu
WHERE time > 1703251234567000000
ORDER BY time DESC
"
# 时间范围之间
curl 'http://localhost:7000/query/mydb' -d "
SELECT * FROM cpu
WHERE time BETWEEN 1703251234567000000 AND 1703251300000000000
"
IN 和 NOT IN
curl 'http://localhost:7000/query/mydb' -d "
SELECT * FROM cpu
WHERE host IN ('server01', 'server02', 'server03')
"
LIKE 模式匹配
curl 'http://localhost:7000/query/mydb' -d "
SELECT * FROM cpu
WHERE host LIKE 'server%'
"
JOIN 查询
INNER JOIN
curl 'http://localhost:7000/query/mydb' -d "
SELECT c.host, c.usage as cpu_usage, m.used as memory_used
FROM cpu c
JOIN memory m ON c.host = m.host
"
LEFT JOIN
curl 'http://localhost:7000/query/mydb' -d "
SELECT c.host, c.usage, m.used
FROM cpu c
LEFT JOIN memory m ON c.host = m.host
"
多表 JOIN
curl 'http://localhost:7000/query/mydb' -d "
SELECT
c.host,
c.usage as cpu_usage,
m.used as memory_used,
d.free as disk_free
FROM cpu c
LEFT JOIN memory m ON c.host = m.host
LEFT JOIN disk d ON c.host = d.host
"
窗口函数
移动平均
curl 'http://localhost:7000/query/mydb' -d "
SELECT
time,
host,
usage,
AVG(usage) OVER (
PARTITION BY host
ORDER BY time
ROWS BETWEEN 2 PRECEDING AND CURRENT ROW
) as moving_avg
FROM cpu
"
排名
curl 'http://localhost:7000/query/mydb' -d "
SELECT
host,
usage,
RANK() OVER (ORDER BY usage DESC) as rank,
DENSE_RANK() OVER (ORDER BY usage DESC) as dense_rank
FROM cpu
"
累计总和
curl 'http://localhost:7000/query/mydb' -d "
SELECT
time,
usage,
SUM(usage) OVER (ORDER BY time) as running_total
FROM cpu
"
Lag / Lead
curl 'http://localhost:7000/query/mydb' -d "
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
"
高级查询
子查询
# 查找高于平均使用率的记录
curl 'http://localhost:7000/query/mydb' -d "
SELECT * FROM cpu
WHERE usage > (SELECT AVG(usage) FROM cpu)
"
# 查找异常值(> 平均值的 2 倍)
curl 'http://localhost:7000/query/mydb' -d "
SELECT * FROM cpu
WHERE usage > 2 * (SELECT AVG(usage) FROM cpu)
"
公用表表达式(CTE)
curl 'http://localhost:7000/query/mydb' -d "
WITH host_stats AS (
SELECT
host,
AVG(usage) as avg_usage,
MAX(usage) as max_usage
FROM cpu
GROUP BY host
)
SELECT * FROM host_stats
WHERE avg_usage > 50
ORDER BY avg_usage DESC
"
CASE 语句
curl 'http://localhost:7000/query/mydb' -d "
SELECT
host,
usage,
CASE
WHEN usage < 30 THEN 'low'
WHEN usage < 70 THEN 'medium'
ELSE 'high'
END as usage_level
FROM cpu
"
UNION
curl 'http://localhost:7000/query/mydb' -d "
SELECT host, 'cpu' as metric, usage as value FROM cpu
UNION ALL
SELECT host, 'memory' as metric, used as value FROM memory
"
时间分桶
# 按分钟分组
curl 'http://localhost:7000/query/mydb' -d "
SELECT
(time / 60000000000) as minute_bucket,
AVG(usage) as avg_usage
FROM cpu
GROUP BY (time / 60000000000)
ORDER BY minute_bucket
"
实用示例
按 CPU 使用率排名前 10 的主机
curl 'http://localhost:7000/query/mydb' -d "
SELECT
host,
AVG(usage) as avg_cpu,
MAX(usage) as max_cpu,
COUNT(*) as samples
FROM cpu
GROUP BY host
ORDER BY avg_cpu DESC
LIMIT 10
"
资源利用率摘要
curl 'http://localhost:7000/query/mydb' -d "
SELECT
c.host,
c.region,
ROUND(AVG(c.usage), 2) as avg_cpu,
ROUND(AVG(m.used / m.total * 100), 2) as avg_memory_pct
FROM cpu c
LEFT JOIN memory m ON c.host = m.host
GROUP BY c.host, c.region
ORDER BY avg_cpu DESC
"
高 CPU 和内存告警
curl 'http://localhost:7000/query/mydb' -d "
SELECT
c.host,
c.usage as cpu_usage,
m.used as memory_used
FROM cpu c
JOIN memory m ON c.host = m.host
WHERE c.usage > 80 AND m.used > 80
ORDER BY c.usage DESC
"
区域对比
curl 'http://localhost:7000/query/mydb' -d "
SELECT
region,
COUNT(DISTINCT host) as hosts,
AVG(usage) as avg_usage,
MIN(usage) as min_usage,
MAX(usage) as max_usage
FROM cpu
GROUP BY region
"
每台主机的最新读数
curl 'http://localhost:7000/query/mydb' -d "
WITH ranked AS (
SELECT
host,
usage,
time,
ROW_NUMBER() OVER (PARTITION BY host ORDER BY time DESC) as rn
FROM cpu
)
SELECT host, usage, time
FROM ranked
WHERE rn = 1
"
使用率变化检测
curl 'http://localhost:7000/query/mydb' -d "
SELECT
time,
host,
usage,
LAG(usage) OVER (PARTITION BY host ORDER BY time) as prev_usage,
usage - LAG(usage) OVER (PARTITION BY host ORDER BY time) as change
FROM cpu
WHERE host = 'server01'
ORDER BY time DESC
LIMIT 20
"
完整工作流示例
# 1. 写入示例数据
curl -X POST http://localhost:7000/write/mydb -d '
cpu,host=server01,region=us-west usage=75.5,temp=45
cpu,host=server02,region=us-west usage=45.2,temp=38
cpu,host=server03,region=us-east usage=89.1,temp=52
memory,host=server01 used=16.5,total=32.0
memory,host=server02 used=24.8,total=64.0
memory,host=server03 used=12.2,total=32.0
'
# 2. 刷新到磁盘
curl -X POST http://localhost:7000/flush
# 3. 查询数据
curl 'http://localhost:7000/query/mydb' -d "
SELECT
c.host,
c.region,
c.usage as cpu_usage,
m.used as memory_used,
CASE
WHEN c.usage > 80 THEN 'critical'
WHEN c.usage > 60 THEN 'warning'
ELSE 'normal'
END as status
FROM cpu c
LEFT JOIN memory m ON c.host = m.host
ORDER BY c.usage DESC
"