timescaledb 几个方便的api

timescaledb 提供了内置的api 操作,方便我们进行操作控制

hypertable 控制api

  • add_dimension

    向hypertable添加一个额外的分片方式,可以做为分片列有时间间隔的以及hash分片

必须参数
名称说明
main_tablehypertable 名称.
column_name使用的分片列
可选参数
名称说明
number_partitionshash 分片个数  column_name. 必须 > 0.
chunk_time_interval每个chunk 的间隔 必须 > 0.
partitioning_func分片函数
if_not_exists避免对于已经存在而进行创建是报错

参考:

SELECT create_hypertable('conditions', 'time');
SELECT add_dimension('conditions', 'location', number_partitions => 4);
  • attach_tablespace

    给hypertable 附加tablespace 信息,数据chunk 会按照此进行存储

必须参数
名称描述
tablespacetablespace
hypertablehypertable 名称
附加tablespace 之前必须创建好,tablespace 可以附加给多个hypertable 
可选参数
名称描述
if_not_attached避免对于已经存在而进行创建是报错

参考:

SELECT attach_tablespace('disk1', 'conditions'); 
SELECT attach_tablespace('disk2', 'conditions', if_not_attached => true);
  • create_hypertable

    创建hypertable

必须参数
名称描述
main_table将要转换为hypertable的表
time_column_name做为时间指的分片列
可选参数
名称描述
partitioning_column分片列
number_partitions分片个数
chunk_time_interval每个chunk的间隔
create_default_indexes分片列索引创建默认是true
if_not_exists避免对于已经存在而进行创建是报错
partitioning_func分片函数
associated_schema_name内部hypertable 关联的schema默认为_timescaledb_internal
associated_table_prefixhypertable chunk 数据表的后缀
migrate_data是否进行数据迁移,会话时间,具体的可以参考官方文档

参考

SELECT create_hypertable('conditions', 'time', chunk_time_interval => 86400000000); 
SELECT create_hypertable('conditions', 'time', chunk_time_interval => interval '1 day');

最佳实践: 具体的可以参考官方文档

  • drop_chunks

    删除chunk 的数据

必须参数
名称描述
older_thanTimestamp of cut-off point for data to be dropped, i.e., anything older than this should be removed.
可选参数
名称描述
table_namehypertable 名称
schema_nameschema 默认是public
cascade是否进行及联数据删除

参考

SELECT drop_chunks(interval '3 months', 'conditions');

分析类函数Analytics

方便进行时序分析的一些函数,比如first 、last、time_bucket

  • first
必须参数
名称描述
valueThe value to return (anyelement)
timeThe timestamp to use for comparison (TIMESTAMP/TIMESTAMPTZ or integer type)

参考

SELECT device_id, first(temp, time) 
FROM metrics 
GROUP BY device_id;
  • last
必须参数
名称参数
valueThe value to return (anyelement)
timeThe timestamp to use for comparison (TIMESTAMP/TIMESTAMPTZ or integer type)

参考

SELECT device_id, time_bucket('5 minutes', time) as interval, last(temp, time) 
FROM metrics WHERE time > now () - interval '1 day'
GROUP BY device_id, interval 
ORDER BY interval DESC;

工具类

比较多,主要有以下几个

  • chunk_relation_size
  • chunk_relation_size_pretty
  • hypertable_relation_size
  • hypertable_relation_size_pretty
  • show_tablespaces

参考资料

https://docs.timescale.com/v0.9/api#hypertable-management

 
 
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值