UNIQUE or PRIMARY KEY index, it must
include the partitioning column (this is usually the time column).
How indexing works
Which column you choose to create your index on depends on what kind of data you have stored. When you create a , set the datatype for thetime column as timestamptz and not timestamp. For more information, see
timestamp.
While it is possible to add an index that does not include the
time column, doing so results in very slow ingest
speeds. For time-series data, indexing on the time column allows one index to be created per .office and garage:
An index on (location, time DESC) is organized like this:
(time DESC, location) is organized like this:
location = garage. Then finish by choosing columns you want to use range operators on, such as time > 0930.
Example: index device metrics
As a more complex example, imagine you have a number of devices tracking 1,000 different retail stores. You have 100 devices per store, and 5 different types of devices. All of these devices report metrics asfloat values, and you
decide to store all the metrics in the same table, like this:
device_id. Or you could query all data for a single
store_id for the last three months.
You want to keep the index on time so that you can quickly filter for a given time range, and add another index on
device_id and store_id. This creates a composite index. A composite index on (store_id, device_id, time) orders
by store_id first. Each unique store_id, will then be sorted by device_id in order. And each entry with the same
store_id and device_id are then ordered by time. To create this index, use this command:
store_id. The index is effective for this query, but could be a
bit bloated; an index on just store_id would probably be more efficient.
time > 10 for one device would be located in a different section than for a
different device. In this case, consider building an index on (store_id, time) instead.
device M is located in a completely
different section of the list for each store_id.
Best practices for indexing
If you have sparse data, with columns that are oftenNULL, you can add a clause to the index, saying
WHERE column IS NOT NULL. This prevents the index from indexing NULL data, which can lead to a more compact and
efficient index. For example:
UNIQUE or PRIMARY KEY index, the index must include the time column and the partitioning
column, if you are using one. For example, a unique index must include at least the (time, location) columns, in
addition to any other columns you want to use. Generally, time-series data uses UNIQUE indexes more rarely than
relational data.
If you do not want to create an index in a single transaction, you can use the CREATE_INDEX
function. This uses a separate function to create an index on each , instead of a single transaction for the
entire . This means that you can perform other actions on the table while the index is being created,
rather than having to wait until index creation is complete.
You can also use the
WITH clause to perform indexing transactions on an individual .Create indexes
You can create an index using theCREATE INDEX command. For example, to create an index that sorts first by
location, then by time, in descending order:
Default indexes
Some indexes are created by default when you perform certain actions on your database. When you create a with a call toCREATE TABLE, a time index is created on
your data. If you want to manually create a time index, you can use this command:
CREATE TABLE or ALTER_TABLE.
If you do not want to create default indexes, you can set create_default_indexes to false when you create a
. For example:
CREATE TABLE ... WITH ..., the default partitioning
column is automatically the first column with a timestamp data type. Also, creates a
columnstore policy that automatically converts your data to the , after an
interval equal to the value of the chunk_interval, defined through compress_after in the
policy. This columnar format enables fast scanning and aggregation, optimizing performance for analytical workloads
while also saving significant storage space. In the conversion, s are compressed by
up to 98%, and organized for efficient, large-scale queries.
You can customize this policy later using alter_job. However, to change after or
created_before, the compression settings, or the the policy is acting on, you must
remove the columnstore policy and add a new one.
You can also manually convert s in a to the .