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
;
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.
timescaleDB
要求Postgresql
数据库版本为12或13,windows版本下载链接;下载完installer运行无脑往下点就行。
然后把 PostgreSQL\13\bin
这个目录添加到环境变量,cmd窗口输入pg_config验证一下,安装tsdb它也会调用这个命令。
tsdb下载地址
下载完解压,以管理员身份运行setup.exe
,一路yes就行。
弄好之后重启服务,在安装路径\PostgreSQL\13\data\postgresql.conf
文件中可以发现
然后启动postgresql 自带的sql shell,或者cmd登录进入数据库输入:
create extension timescaledb;
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
里面的可选参数设定可以参考链接。
#修改
ALTER TABLE conditions
ADD COLUMN humidity DOUBLE PRECISION NULL;
#删除
DROP TABLE conditions;
To be continued…