如何在tdengine中插入超出keep限制的时间戳

how to insert timestamp beyond the limitation of keep in tdengine

我发现tdengine有个参数will create database。此定义“KEEP 参数指的是保存修改后的数据文件的天数。”来自 tdengine 的网站 https://www.taosdata.com/en/documentation/taos-sql#management。我觉得这个参数很有用,不需要删除历史数据。所以我创建了一个只保留 10 天的数据库。

CREATE DATABASE IF NOT EXISTS db_keep KEEP 10 PRECISION 'ms' ;
create table test_keep(ts timestamp,desc nchar(20));

创建数据库和 table 后,我尝试将一些数据插入 table。以下是我的插入 sqls.

insert into test_keep values(now,'now');
insert into test_keep values('2021-08-31 10:28:53.521','yesterday');
insert into test_keep values('2021-09-02 10:28:53.521','tomorrow');
insert into test_keep values('2021-08-25 10:28:53.521','6 days before');
insert into test_keep values('2021-09-20 12:28:53.521','20 days later');
insert into test_keep values('2021-08-21 10:28:53.521','10 days before');
insert into test_keep values('2021-08-11 10:28:53.521','20 days before');

而后三个 sql 执行错误“数据库错误:时间戳数据超出范围”

taos> insert into test_keep values(now,'now'); Query OK, 1 of 1 row(s) in database (1.024000s)

taos> insert into test_keep values('2021-08-31 10:28:53.521','yesterday'); Query OK, 1 of 1 row(s) in database (0.006000s)

taos> insert into test_keep values('2021-09-02 10:28:53.521','tomorrow'); Query OK, 1 of 1 row(s) in database (0.004000s)

taos> insert into test_keep values('2021-08-25 10:28:53.521','6 days before'); Query OK, 1 of 1 row(s) in database (0.004000s)

taos> insert into test_keep values('2021-09-20 12:28:53.521','20 days later');

DB error: Timestamp data out of range (0.005000s) taos> insert into test_keep values('2021-08-21 10:28:53.521','10 days before');

DB error: Timestamp data out of range (0.004000s) taos> insert into test_keep values('2021-08-11 10:28:53.521','20 days before');

DB error: Timestamp data out of range (0.004000s) taos>

我觉得这个是因为我的keep太小了,所以我把它弄大了。

ALTER DATABASE db_keep KEEP 365;

然后我再次尝试插入失败的 sql 我发现几天后无法插入数据。

taos> insert into test_keep values('2021-09-20 12:28:53.521','20 days later');

DB error: Timestamp data out of range (0.005000s) taos> insert into test_keep values('2021-08-21 10:28:53.521','10 days before'); Query OK, 1 of 1 row(s) in database (0.004000s)

taos> insert into test_keep values('2021-08-11 10:28:53.521','20 days before'); Query OK, 1 of 1 row(s) in database (0.004000s)

想问一下keep怎么用,怎么限制数据的时间戳?

对于过去的数据,时间戳值不能超过(current_time - keep)。同时,对于未来的数据,时间戳值不能超过(current time + days).

由于我的声望不够,无法在这里插入图片,详情请参考下面的参考资料link(灰色表示无法插入)。

参考:

  1. https://segmentfault.com/a/1190000040617572

对于数据库中的时间戳,两种配置方案最合理:

  1. keep:数据库最长保存天数参考当前时间戳(默认3650天)。当持久文件中的“最新”时间戳超出范围时,持久文件将被删除。早于 now - keep 的时间戳将被视为 Timestamp out of range.
  2. days:文件中存储数据的时间范围,默认10天。较新的时间戳不会大于 now+ days.

因此,数据库中可接受的时间戳范围将为 [now - keep, now + days]