Skip to content

Commit c4a4108

Browse files
authored
docs: add spatial index DDL (#3292)
1 parent d60ae25 commit c4a4108

12 files changed

Lines changed: 507 additions & 0 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"label": "空间索引 (Spatial Index)",
3+
"position": 13
4+
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
---
2+
title: CREATE SPATIAL INDEX
3+
sidebar_position: 1
4+
---
5+
6+
在 Databend 中创建新的空间索引。
7+
8+
## 语法
9+
10+
```sql
11+
CREATE [ OR REPLACE ] SPATIAL INDEX [IF NOT EXISTS] <index>
12+
ON [<database>.]<table>( <geometry_column>[, <geometry_column> ...] )
13+
```
14+
15+
| 参数 | 描述 |
16+
|-----------|-------------|
17+
| `[ OR REPLACE ]` | 如果索引已存在,则替换已有索引。 |
18+
| `[ IF NOT EXISTS ]` | 仅在同名索引不存在时创建索引。 |
19+
| `<index>` | 空间索引的名称。 |
20+
| `[<database>.]<table>` | 包含被索引列的表。 |
21+
| `<geometry_column>` | 要加入索引的 `GEOMETRY` 列。语句中列名不能重复。 |
22+
23+
## 使用说明
24+
25+
- 空间索引仅支持 Fuse 表。
26+
- 空间索引仅支持 `GEOMETRY` 列,不支持 `GEOGRAPHY` 列。
27+
- 一个空间索引定义中可以包含多个列,这些列都必须是 `GEOMETRY` 类型。
28+
29+
## 示例
30+
31+
创建包含空间列的表:
32+
33+
```sql
34+
CREATE TABLE stores (
35+
store_id INT,
36+
store_name STRING,
37+
location GEOMETRY
38+
) ENGINE = FUSE;
39+
```
40+
41+
`location` 列上创建空间索引:
42+
43+
```sql
44+
CREATE SPATIAL INDEX stores_location_idx ON stores(location);
45+
```
46+
47+
查看表定义:
48+
49+
```sql
50+
SHOW CREATE TABLE stores;
51+
52+
┌──────────────────────────────────────────────────────────────────────────────────────────────┐
53+
│ Table │ Create Table │
54+
├──────────────────────────────────────────────────────────────────────────────────────────────┤
55+
│ stores │ CREATE TABLE stores ( │
56+
│ │ store_id INT NULL, │
57+
│ │ store_name VARCHAR NULL, │
58+
│ │ location GEOMETRY NULL, │
59+
│ │ SYNC SPATIAL INDEX stores_location_idx (location) │
60+
│ │ ) ENGINE=FUSE │
61+
└──────────────────────────────────────────────────────────────────────────────────────────────┘
62+
```
63+
64+
插入一些用于空间过滤的示例数据:
65+
66+
```sql
67+
INSERT INTO stores VALUES
68+
(1, 'Starbucks', TO_GEOMETRY('POINT(10 10)')),
69+
(2, 'Costa', TO_GEOMETRY('POINT(11 11)')),
70+
(3, 'Gong Cha', TO_GEOMETRY('POINT(20 20)')),
71+
(4, 'Dunkin', TO_GEOMETRY('POINT(-10 -10)'));
72+
```
73+
74+
### 使用 `ST_WITHIN``ST_INTERSECTS``ST_CONTAINS` 过滤
75+
76+
这些谓词是常见的 geofence 场景过滤条件,通常都可以从空间索引中受益。
77+
78+
```sql
79+
-- 查询位于多边形内部的位置
80+
SELECT store_id, store_name
81+
FROM stores
82+
WHERE ST_WITHIN(
83+
location,
84+
TO_GEOMETRY('POLYGON((9 9, 9 12, 12 12, 12 9, 9 9))')
85+
)
86+
ORDER BY store_id;
87+
```
88+
89+
```sql
90+
-- 查询与多边形相交的位置
91+
SELECT store_id, store_name
92+
FROM stores
93+
WHERE ST_INTERSECTS(
94+
location,
95+
TO_GEOMETRY('POLYGON((9 9, 9 12, 12 12, 12 9, 9 9))')
96+
)
97+
ORDER BY store_id;
98+
```
99+
100+
```sql
101+
-- 查询被多边形包含的点
102+
SELECT store_id, store_name
103+
FROM stores
104+
WHERE ST_CONTAINS(
105+
TO_GEOMETRY('POLYGON((9 9, 9 12, 12 12, 12 9, 9 9))'),
106+
location
107+
)
108+
ORDER BY store_id;
109+
```
110+
111+
### 使用 `ST_DWITHIN` 按距离过滤
112+
113+
`ST_DWITHIN` 适合做半径范围查询,例如“查找附近位置”。
114+
115+
```sql
116+
SELECT store_id, store_name
117+
FROM stores
118+
WHERE ST_DWITHIN(
119+
location,
120+
TO_GEOMETRY('POINT(10 10)'),
121+
1.5
122+
)
123+
ORDER BY store_id;
124+
```
125+
126+
### 在空间 Join 中过滤
127+
128+
当 Join 条件是受支持的空间谓词时,空间索引同样有帮助。
129+
130+
```sql
131+
CREATE TABLE districts (
132+
district_id INT,
133+
district_name STRING,
134+
geom GEOMETRY
135+
) ENGINE = FUSE;
136+
137+
INSERT INTO districts VALUES
138+
(1, 'Central', TO_GEOMETRY('POLYGON((8 8, 8 13, 13 13, 13 8, 8 8))')),
139+
(2, 'West', TO_GEOMETRY('POLYGON((-2 -2, -2 2, 2 2, 2 -2, -2 -2))'));
140+
```
141+
142+
```sql
143+
SELECT d.district_name, s.store_name
144+
FROM districts AS d
145+
JOIN stores AS s
146+
ON ST_WITHIN(s.location, d.geom)
147+
ORDER BY d.district_name, s.store_name;
148+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: DROP SPATIAL INDEX
3+
sidebar_position: 3
4+
---
5+
6+
在 Databend 中删除空间索引。
7+
8+
## 语法
9+
10+
```sql
11+
DROP SPATIAL INDEX [IF EXISTS] <index> ON [<database>.]<table>
12+
```
13+
14+
## 示例
15+
16+
```sql
17+
CREATE TABLE stores (
18+
store_id INT,
19+
store_name STRING,
20+
location GEOMETRY,
21+
SPATIAL INDEX location_idx (location)
22+
) ENGINE = FUSE;
23+
24+
DROP SPATIAL INDEX location_idx ON stores;
25+
```
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
title: 空间索引(Spatial Index)
3+
---
4+
5+
Databend 中的空间索引可加速 `GEOMETRY` 列上的空间谓词过滤。它面向 Fuse 表设计,能够在执行精确空间函数前帮助优化器先做 block pruning。
6+
7+
:::tip[关键特性:自动索引构建]
8+
空间索引会在索引创建后的新数据写入过程中自动维护。如果你是在表中已有数据后再创建索引,可以使用 `REFRESH SPATIAL INDEX` 对历史数据进行回填。
9+
:::
10+
11+
## 空间索引管理
12+
13+
| 命令 | 描述 |
14+
|---------|-------------|
15+
| [CREATE SPATIAL INDEX](create-spatial-index.md) | 在一个或多个 `GEOMETRY` 列上创建空间索引 |
16+
| [REFRESH SPATIAL INDEX](refresh-spatial-index.md) | 为索引创建前已有的数据回填空间索引 |
17+
| [DROP SPATIAL INDEX](drop-spatial-index.md) | 从表中删除空间索引 |
18+
19+
## 支持的谓词
20+
21+
Databend 可以使用空间索引来加速以下空间谓词构造的查询:
22+
23+
- `ST_CONTAINS`
24+
- `ST_INTERSECTS`
25+
- `ST_WITHIN`
26+
- `ST_DWITHIN`
27+
28+
## 限制
29+
30+
- 空间索引仅支持 Fuse 表。
31+
- 被索引的列必须是 `GEOMETRY` 类型。
32+
- 不支持 `GEOGRAPHY` 列。
33+
34+
## 相关主题
35+
36+
- [地理空间函数](/sql/sql-functions/geospatial-functions)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
title: REFRESH SPATIAL INDEX
3+
sidebar_position: 2
4+
---
5+
6+
Databend 会在 `SYNC` 模式下对新写入的数据自动维护空间索引。`REFRESH SPATIAL INDEX` 主要用于在索引创建之前表中已经存在数据时,对这些历史数据进行回填。
7+
8+
## 语法
9+
10+
```sql
11+
REFRESH SPATIAL INDEX <index> ON [<database>.]<table> [LIMIT <limit>]
12+
```
13+
14+
| 参数 | 描述 |
15+
|-----------|-------------|
16+
| `<limit>` | 指定本次刷新最多处理的行数。如果不指定,则处理表中的全部数据。 |
17+
18+
## 示例
19+
20+
```sql
21+
-- 先创建已经有数据的表
22+
CREATE TABLE IF NOT EXISTS stores (
23+
store_id INT,
24+
location GEOMETRY
25+
) ENGINE = FUSE;
26+
27+
INSERT INTO stores VALUES
28+
(1, TO_GEOMETRY('POINT(10 10)')),
29+
(2, TO_GEOMETRY('POINT(20 20)'));
30+
31+
-- 在表创建之后再添加空间索引
32+
CREATE SPATIAL INDEX stores_location_idx ON stores(location);
33+
34+
-- 回填历史数据,让索引覆盖已有行
35+
REFRESH SPATIAL INDEX stores_location_idx ON stores;
36+
37+
-- 之后的新写入会在 SYNC 模式下自动维护索引
38+
```

docs/cn/sql-reference/10-sql-commands/00-ddl/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ title: DDL(Data Definition Language)命令
2222
| **[聚合索引 (Aggregating Index)](07-aggregating-index/index.md)** | 预计算聚合以加快查询速度 |
2323
| **[倒排索引 (Inverted Index)](07-inverted-index/index.md)** | 用于文本列的全文搜索索引 |
2424
| **[Ngram 索引 (Ngram Index)](07-ngram-index/index.md)** | 用于 LIKE 模式的子字符串搜索索引 |
25+
| **[空间索引 (Spatial Index)](07-spatial-index/index.md)** | 用于 `GEOMETRY` 列的空间裁剪索引 |
26+
| **[向量索引 (Vector Index)](07-vector-index/index.md)** | 向量嵌入的相似度搜索索引 |
2527
| **[虚拟列 (Virtual Column)](07-virtual-column/index.md)** | 将 JSON 字段提取并索引为虚拟列 |
2628

2729
## 安全和访问控制
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"label": "Spatial Index",
3+
"position": 13
4+
}

0 commit comments

Comments
 (0)