InfluxDB data Retention Policies and influxdbretention
InfluxDB can process thousands of data records per second. To save all the data, it will occupy a lot of storage space. Sometimes we may not need to store all the historical data. Therefore, influxDB introduces a Retention policy to allow us to customize the Retention time of data. For more details about InfluxDB, see the InfluxDB series learning tutorial directory.
InfluxDB technology exchange group: 580487672 (click to join)
I. Description of InfluxDB data retention policies
The Data Retention Policy (RP) of InfluxDB is used to define the time when data is stored in InfluxDB, or to define the data stored during a certain period.
A database can have multiple retention policies, but each policy must be unique.
Ii. InfluxDB data retention policy objective
InfluxDB does not provide data deletion operations. Therefore, the data retention policy is defined to control the data volume.
Therefore, the purpose of defining a data retention policy is to enable InfluxDB to know which data can be discarded to process data more efficiently.
Iii. InfluxDB data retention policy operations
1) query Policy
You can use the following statement to view the existing database policies:
> SHOW RETENTION POLICIES ON telegrafname duration shardGroupDuration replicaN defaultdefault 0 168h0m0s 1 true
As you can see, telegraf has only one policy. The meaning of each field is as follows:
Name -- name, which is default
Duration-duration, 0 indicates Unlimited
ShardGroupDuration -- the storage time of shardGroup. shardGroup is a basic storage structure of InfluxDB. The query efficiency of data later than this time should be reduced.
ReplicaN -- Full name: REPLICATION, number of replicas
Default -- whether it is the default policy
2) create a policy
> CREATE RETENTION POLICY "2_hours" ON "telegraf" DURATION 2h REPLICATION 1 DEFAULT> SHOW RETENTION POLICIES ON telegrafname duration shardGroupDuration replicaN defaultdefault 0 168h0m0s 1 false2_hours 2h0m0s 1h0m0s 1 true
The preceding statement can be used to add a policy. In this example, a two-hour policy is added to the telegraf database. The Policy Name Is 2_hours, the duration is 2 hours, the copy is 1, and the Default policy is set.
Because the policy named default is no longer a default policy, you must explicitly add the Policy Name "default" When querying tables using the default policy ".
> select * from "default".cpu limit 2name: cpu---------time cpu host host_id usage_guest usage_guest_nice usage_idle usage_iowait usage_irq usage_nice usage_softirq usage_steal usage_system usage_user1467884670000000000 cpu-total ResourcePool-0246-billing07 0 0 99.79994164175388 0 0 0.06251823446523729 0 0 0.12920435125646068 0.0083357646034517271467884670000000000 cpu9 billing07 0 0 97.79338014069532 1.8054162487519367 0 0 0 0 0.10030090272883943 0.3009027081135398
3) modify the policy
Modify the policy using the following statement:
> ALTER RETENTION POLICY "2_hours" ON "telegraf" DURATION 4h DEFAULT> show retention POLICIES on telegrafname duration shardGroupDuration replicaN defaultdefault 0 168h0m0s 1 false2_hours 4h0m0s 1h0m0s 1 true
We can see that the modified policy has changed.
4) deletion policy
The deletion of policies in InfluxDB is as follows:
> drop retention POLICY "2_hours" ON "telegraf"> show retention POLICIES on telegrafname duration shardGroupDuration replicaN defaultdefault 0 168h0m0s 1 false
We can see that the policy named 2_hours has been deleted.
IV. Other Instructions
POLICY: the keyword "POLICY" should be in upper case and the keyword "POLICY" should be in lower case.
When a table uses a policy that is not the Default policy, you must explicitly specify the policy name during the operation. Otherwise, an error occurs.
Now, we will introduce you to the InfluxDB Retention Policies first. If you have any questions, please leave a message to discuss them.
For more details about InfluxDB, see the InfluxDB series learning tutorial directory.
InfluxDB technology exchange group: 580487672 (click to join)