zl程序教程

您现在的位置是:首页 >  系统

当前栏目

windows 安装、使用 timescaleDB

Windows安装 使用
2023-09-27 14:27:14 时间

timescaleDB(下称tsdb)主要用来存储 time-series data,对于这个概念,tsdb官方给出的定义为:Time-series data is data that collectively represents how a system, process, or behavior changes over time.
像tsdb这样的时序型数据库利用了time-series data的以下特点:

  • 以时间为中心:总有一个timestamp字段;
  • 写多改少:几乎都是insert操作
  • 最近数据比较重要:我们很少更新或回填距现在时间间隔较长的缺失数据;

time-series data和一般的data不一样的关键点在于,数据库time-series data的操作或者改变基本都是inserts 而不是overwrite;

How TSDB manages time-series data

TimescaleDB uses hypertables to store time-series data. TimescaleDB automatically partitions data in hypertables into smaller child tables called chunks. The chunks represent data for a given time period, which makes it easier to query and manage over time. For example, if you wanted to query data from 10am to 11am, instead of scanning your entire database, TimescaleDB would scan the specific chunks that contain data for just that period. All the interaction with the database still occurs on the hypertable using SQL, but TimescaleDB partitions the data to make large queries more efficient.
Many features in TimescaleDB rely on chunks, including continuous aggregates, data retention, and native compression. Native compression is particularly helpful with large time-series datasets. Time-series data can be relentless in quantity and speed, and difficult to store and query without a purpose-built time-series database. You can use TimescaleDB compression to save as much as 97% of your disk space for the same amount of data, and usually increase the speed of your queries over time.

安装Postgresql

timescaleDB要求Postgresql数据库版本为12或13,windows版本下载链接;下载完installer运行无脑往下点就行。

然后把 PostgreSQL\13\bin 这个目录添加到环境变量,cmd窗口输入pg_config验证一下,安装tsdb它也会调用这个命令。在这里插入图片描述

安装tsdb

官网教程链接:安装前的准备:
在这里插入图片描述
大意是:

  • 安装VS2015或以后版本
  • postgresql 版本要12或13
  • 环境变量配置好
  • 电脑管理员身份执行安装

tsdb下载地址
下载完解压,以管理员身份运行setup.exe,一路yes就行。

弄好之后重启服务,在安装路径\PostgreSQL\13\data\postgresql.conf文件中可以发现
在这里插入图片描述
然后启动postgresql 自带的sql shell,或者cmd登录进入数据库输入:

 create extension timescaledb;

在这里插入图片描述
然后就可以创建表,转为超表了。

基操

1.创建超表

CREATE TABLE conditions (
    time        TIMESTAMPTZ       NOT NULL,
    location    TEXT              NOT NULL,
    temperature DOUBLE PRECISION  NULL
);
SELECT create_hypertable('conditions', 'time');

create_hypertable函数两个必填:第一个是表名,第二个是时间字段。

#如果之前的普通表中有数据,在调用函数转超表时需要设置`migrate_data`参数为`true`
SELECT create_hypertable('conditions','time', migrate_data => true)

#设置一个chunk时间范围为1天
SELECT * FROM create_hypertable('conditions', 'time', chunk_time_interval => INTERVAL '1 day');

#手动删除:超过24小时的chunk删除
SELECT drop_chunks('conditions', INTERVAL '24 hours');
#自动删除:
SELECT add_retention_policy('conditions', INTERVAL '6 months');

关于create_hypertable里面的可选参数设定可以参考链接

2.修改、删除超表

#修改
ALTER TABLE conditions
  ADD COLUMN humidity DOUBLE PRECISION NULL;

#删除
DROP TABLE conditions;

To be continued…