27-index.md 2.4 KB
Newer Older
1
---
D
danielclow 已提交
2 3 4
title: Indexing
sidebar_label: Indexing
description: This document describes the SQL statements related to indexing in TDengine.
5 6
---

dengyihao's avatar
dengyihao 已提交
7
TDengine supports SMA and tag indexing.
8

9
## Create an Index
10 11

```sql
dengyihao's avatar
dengyihao 已提交
12
CREATE INDEX index_name ON tb_name (col_name [, col_name] ...)
13 14 15 16 17 18 19 20 21 22

CREATE SMA INDEX index_name ON tb_name index_option

index_option:
    FUNCTION(functions) INTERVAL(interval_val [, interval_offset]) [SLIDING(sliding_val)] [WATERMARK(watermark_val)] [MAX_DELAY(max_delay_val)]

functions:
    function [, function] ...
```

23
### SMA Indexing
24

25
Performs pre-aggregation on the specified column over the time window defined by the INTERVAL clause. The type is specified in functions_string. SMA indexing improves aggregate query performance for the specified time period. One supertable can only contain one SMA index.
26

27 28 29
- The max, min, and sum functions are supported.
- WATERMARK: Enter a value between 0ms and 900000ms. The most precise unit supported is milliseconds. The default value is 5 seconds. This option can be used only on supertables.
- MAX_DELAY: Enter a value between 1ms and 900000ms. The most precise unit supported is milliseconds. The default value is the value of interval provided that it does not exceed 900000ms. This option can be used only on supertables. Note: Retain the default value if possible. Configuring a small MAX_DELAY may cause results to be frequently pushed, affecting storage and query performance.
30

K
kailixu 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
```sql
DROP DATABASE IF EXISTS d0;
CREATE DATABASE d0;
USE d0;
CREATE TABLE IF NOT EXISTS st1 (ts timestamp, c1 int, c2 float, c3 double) TAGS (t1 int unsigned);
CREATE TABLE ct1 USING st1 TAGS(1000);
CREATE TABLE ct2 USING st1 TAGS(2000);
INSERT INTO ct1 VALUES(now+0s, 10, 2.0, 3.0);
INSERT INTO ct1 VALUES(now+1s, 11, 2.1, 3.1)(now+2s, 12, 2.2, 3.2)(now+3s, 13, 2.3, 3.3);
CREATE SMA INDEX sma_index_name1 ON st1 FUNCTION(max(c1),max(c2),min(c1)) INTERVAL(5m,10s) SLIDING(5m) WATERMARK 5s MAX_DELAY 1m;
-- query from SMA Index
ALTER LOCAL 'querySmaOptimize' '1';
SELECT max(c2),min(c1) FROM st1 INTERVAL(5m,10s) SLIDING(5m);
SELECT _wstart,_wend,_wduration,max(c2),min(c1) FROM st1 INTERVAL(5m,10s) SLIDING(5m);
-- query from raw data
ALTER LOCAL 'querySmaOptimize' '0';
```

49
## Delete an Index
50 51 52 53 54

```sql
DROP INDEX index_name;
```

55
## View Indices
56 57 58 59 60

````sql
SHOW INDEXES FROM tbl_name [FROM db_name];
````

61
Shows indices that have been created for the specified database or table.