1.造数
taosBenchmark --start-timestamp=1600000000000 --tables=100 --records=10000000 --time-step=10000
让 taosBenchmark 在 TDENGINE 中生产一个用于测试的测试数据库,产生共10亿条时序数据,时序数据
的数据戳从1600000000000 ,2017-09-13T20:26:40+08:00
包含100台设备(子表),每台设备有1000W条记录。时序数据的采集周期是每台设备10s/条。
drop table meters;
#删除超级表之后,子表也被删除。
select * from meters where voltage > 10 order by ts desc limit 5;
taos> select * from meters where voltage > 10 order by ts desc limit 5;
ts | current | voltage | phase | groupid | location |
=================================================================================================================================
2021-10-28 22:50:00.000 | 2.6984551 | 18 | 0.8861520 | 3 | California.SantaClara |
2021-10-28 22:49:20.000 | 2.9547670 | 18 | 0.9949220 | 3 | California.SantaClara |
2021-10-28 22:48:40.000 | 2.0274889 | 12 | 0.6624960 | 3 | California.SantaClara |
2021-10-28 22:48:10.000 | -2.8540101 | 14 | -0.9746700 | 3 | California.SantaClara |
2021-10-28 22:47:50.000 | 3.0545650 | 18 | 0.9848550 | 3 | California.SantaClara |
Query OK, 5 row(s) in set (0.007616s)
2.聚合查询
TDENGINE 支持通过group by 子句对数据进行聚合查询。
select groupid,avg(voltage) from meters
where ts>='2022-01-01T00:00:00+08:00' and ts < '2023-01-01T00:00:00+08:00'
group by groupid;
groupid | avg(voltage) |
==========================================
8 | 9.777777777777779 |
5 | 9.422222222222222 |
1 | 8.845833333333333 |
9 | 8.911111111111111 |
6 | 9.311111111111112 |
4 | 8.888895638315381 |
10 | 8.688888888888888 |
3 | 8.891666666666667 |
Query OK, 8 row(s) in set (0.386213s)
group by 子句在聚合数据时,并不保证结果集按照特定顺序排列。为了获取有序的结果集,
可以使用 order by 子句对结果进行排序。
3.TDENGINE 内置的聚合函数。
apercentile :统计表、超级表中指定列的值的近似百分比分位数,与 percentile 函数相似,返回近似结果。
avg:平均值。
count: 记录行数。
elapsed :统计周期内连续的时间长度,并与 twa 函数配合使用,以计算统计曲线下的面积。
当使用 interval 子句指定窗口时, elapsed 函数会返回给定时间范围内的每个窗口内有
数据覆盖的时间范围。如果没有使用interval 子句, elapsed 函数将返回整个给定时间
范围内的有数据覆盖的时间范围。 elapsed 函数返回的并不是时间范围的绝对值,而是将绝对值除以
time_unit 所得到的单位个数。
leastsquares: 统计表中某列值的拟合直线方程。 star_val :是自变量初始值,step_val 是自变量的步长值。
spread:统计某列的最大值和最小值之差。
stdev :统计表的某列的均方差。
sum: 求和
hyperloglog :返回某列的基数,即该列中唯一值的数量。这种算法在处理大量数据时具有显著的优势,因为它
可以显著降低内存占用。hyperloglog 返回的基数是一个估算值,其标准误差约为 0.81%;
在数据量较少的情况下, hyperloglog 的准确率可能会有所下降。
可以使用 select count(data) from (selct unique(col) as data from table); 替代。
histgram:统计数据按照用户指定区间的分布。
percentile:统计表中某列的百分比分位数。