The part of InfluxDB’s data structure that describes for how long InfluxDB keeps data (duration), how many copies of this data is stored in the cluster (replication factor), and the time range covered by shard groups (shard group duration). RPs are unique per database and along with the measurement and tag set define a series.
下面简单谈一下我对这句话的理解。retention policy描述了influxdb中的数据会保留多长时间、数据保留几个副本(开源版的只能保留一个副本),以及每个shard保存多长时间的数据。每个influxdb数据库都有一个独立的retention policy。这里面涉及到几个基本概念,下面描述一下。
influxdb官方建议,如果保留期限低于2天,shard duration设置为1h,如果保留期限位于2天到6个月之间,shard duration设置为1d,如果保留期限大于6个月,shard duration设置为7d,
当执行create database
时,会创建一个名叫autogen
的retention policy,它会永久保留数据
> create database testdb
> use testdb
Using database testdb
> show retention policies
name duration shardGroupDuration replicaN default
---- -------- ------------------ -------- -------
autogen 0s 168h0m0s 1 true
可以通过create retention policy
的命令来创建一个新的retention policy
CREATE RETENTION POLICY "one_month" ON "testdb" DURATION 30d REPLICATION 1 DEFAULT
然后再查看一下retention policy
> show retention policies
name duration shardGroupDuration replicaN default
---- -------- ------------------ -------- -------
autogen 0s 168h0m0s 1 false
one_month 720h0m0s 24h0m0s 1 true
需要注意的是,如果切换数据库的retention policy的话,已有的数据会被全部删掉。
如果想修改retention policy的数据保留时间,可以使用alter retention policy
语句
> alter retention policy autogen on mydb duration 30d REPLICATION 1 SHARD DURATION 1d default
> show retention policies
name duration shardGroupDuration replicaN default
---- -------- ------------------ -------- -------
autogen 720h0m0s 24h0m0s 1 true
更新retention policy的保留时间的话,数据不会全部丢失。
关于retention policy,最好的方式是在创建数据库时就考虑清楚数据要保留多长时间。简单来讲,可以通过下面的方式创建一个保留30天的数据库,并且,influxdb还会自动生成一个比较合理的shardGroupDuration,此时是1天。
> create database testdb2 with duration 30d
> show retention policies on testdb2
name duration shardGroupDuration replicaN default
---- -------- ------------------ -------- -------
autogen 720h0m0s 24h0m0s 1 true
https://docs.influxdata.com/influxdb/v1.7/query_language/database_management/
https://stackoverflow.com/questions/41620595/how-to-set-default-retention-policy-and-duration-for-influxdb-via-configuration
https://dzone.com/articles/simplifying-influxdb-shards-and-retention-policies
https://stackoverflow.com/questions/39878027/influxdb-whats-shard-group-duration