Databases 10 min read

Master InfluxDB: From Basics to Advanced Queries and Retention Policies

This guide explains InfluxDB's architecture, data model, CRUD commands, measurement handling, retention policies, query syntax, time‑zone considerations, and service management, providing a comprehensive tutorial for developers working with time‑series databases.

Efficient Ops
Efficient Ops
Efficient Ops
Master InfluxDB: From Basics to Advanced Queries and Retention Policies

What is InfluxDB

InfluxDB is an open‑source time‑series database developed by InfluxData, written in Go, designed for high‑performance storage and querying of time‑series data. It is widely used for system monitoring, IoT real‑time data, and similar scenarios.

Key Features

Single‑binary deployment without external dependencies, leveraging Go’s features.

SQL‑like query language with many built‑in functions.

Data model composed of measurements, tag sets, field sets and a timestamp.

Data Model Details

Measurement : a string that identifies the type of record, e.g.

cpu_load

or

average_temperature

.

Tag set : a schema‑free collection of key‑value pairs that are indexed by default.

Field set : a collection of key‑value pairs storing the actual values (int64, float64, string, boolean); fields are not indexed.

Timestamp : the time attribute of the record; if omitted, the server uses the write time.

Supports data insertion and queries over HTTP, TCP or UDP.

Retention Policies can be defined to automatically delete or down‑sample data older than a specified duration.

CRUD Operations

Enter the InfluxDB CLI:

<code>influx -precision rfc3339</code>

Show databases:

show databases

Create database:

create database shhnwangjian

Drop database:

drop database shhnwangjian

Use database:

use shhnwangjian

Measurements (Tables) Operations

InfluxDB does not have explicit tables; measurements serve the same purpose.

Show all measurements:

SHOW MEASUREMENTS

Create measurement by inserting data, e.g.:

<code>insert disk_free,hostname=server01 value=442221834240i
insert cpu_virtual_used_num,host=1 value=4 1556593150</code>

In the line above,

disk_free

is the measurement name,

hostname

is a tag, and

value=…

is a field. A custom timestamp can be appended.

<code>insert disk_free,hostname=server01 value=442221834240i 1435362189575692182</code>

Drop measurement:

drop measurement disk_free

Retention Policies

Retention Policies (RP) define how long data is kept.

Show RPs:

show retention policies on "db_name"

Create RP:

create retention policy "rp_name" on "db_name" duration 3w replication 1 default

Alter RP:

alter retention policy "rp_name" on "db_name" duration 30d default

Drop RP:

drop retention policy "rp_name" on "db_name"

Parameters: duration (e.g., 3w, 1h), replication factor, and the

default

flag.

Querying Data

<code>select * from cpu_virtual_used_num</code>

Inserting Data

Inserting data also creates the measurement if it does not exist:

<code>insert disk_free,hostname=server01 value=442221834240i
insert cpu_virtual_used_num,host=470b14f0-e869-43ed-a8e6-fd634258271f,hostname=server01 value=0.3 1557023160</code>

Deleting Data

Direct row deletion is not supported; data is removed by configuring a Retention Policy with a suitable duration.

Inspecting Measurement Schema

Show tag keys:

<code>show tag keys from cluster_metric</code>

Show field keys:

<code>show field keys from cluster_metric</code>

Time‑zone Handling

InfluxDB stores timestamps in UTC. Queries can use different time formats:

epoch_time

(nanoseconds, microseconds, etc.)

rfc3339_date_time_string

– e.g., ‘YYYY‑MM‑DDTHH:MM:SS.nnnnnnnnnZ’

rfc3339_like_date_time_string

– e.g., ‘YYYY‑MM‑DD HH:MM:SS.nnnnnnnnn’

Precision can be adjusted in queries, e.g., using seconds (

... >= 1435333209s

) or milliseconds (

... >= 1435333209000ms

).

To query in Beijing time, add the

tz('Asia/Shanghai')

clause:

<code>select * from cpu_virtual_used_num where time >= '2018-11-23 14:30:39' and time <= '2019-11-23 14:32:32' tz('Asia/Shanghai')</code>

Service Management

<code>sudo service influxdb start
service influxdb restart
exit   // return to normal user</code>
time-series databaseInfluxDBTime zonesData insertionRetention PoliciesSQL-like queries
Efficient Ops
Written by

Efficient Ops

This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.