You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/cosmos-db/index-overview.md
+48-8Lines changed: 48 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ description: Understand how indexing works in Azure Cosmos DB.
4
4
author: ThomasWeiss
5
5
ms.service: cosmos-db
6
6
ms.topic: conceptual
7
-
ms.date: 09/10/2019
7
+
ms.date: 10/11/2019
8
8
ms.author: thweiss
9
9
---
10
10
@@ -59,26 +59,49 @@ When an item is written, Azure Cosmos DB effectively indexes each property's pat
59
59
60
60
## Index kinds
61
61
62
-
Azure Cosmos DB currently supports three kinds of indexes:
62
+
Azure Cosmos DB currently supports three kinds of indexes.
63
63
64
-
The **range** index kind is used for:
64
+
### Range Index
65
+
66
+
**Range** index is based on an ordered tree-like structure. The range index kind is used for:
65
67
66
68
- Equality queries:
67
69
68
70
```sql
69
71
SELECT*FROM container c WHEREc.property='value'
70
72
```
71
73
74
+
```sql
75
+
SELECT*FROM c WHEREc.propertyIN ("value1", "value2", "value3")
76
+
```
77
+
78
+
Equality match on an array element
79
+
```sql
80
+
SELECT*FROM c WHERE ARRAY_CONTAINS(c.tags, "tag1”)
81
+
```
82
+
72
83
- Range queries:
73
84
74
85
```sql
75
86
SELECT * FROM container c WHERE c.property > 'value'
76
87
```
77
88
(works for `>`, `<`, `>=`, `<=`, `!=`)
78
89
90
+
- Checking for the presence of a property:
91
+
92
+
```sql
93
+
SELECT * FROM c WHERE IS_DEFINED(c.property)
94
+
```
95
+
96
+
- String prefix matches (CONTAINS keyword will not leverage the range index):
97
+
98
+
```sql
99
+
SELECT * FROM c WHERE STARTSWITH(c.property, "value")
100
+
```
101
+
79
102
- `ORDER BY` queries:
80
103
81
-
```sql
104
+
```sql
82
105
SELECT * FROM container c ORDER BY c.property
83
106
```
84
107
@@ -90,23 +113,33 @@ The **range** index kind is used for:
90
113
91
114
Range indexes can be used on scalar values (string or number).
92
115
93
-
The **spatial** index kind is used for:
116
+
### Spatial index
117
+
118
+
**Spatial** indices enable efficient queries on geospatial objects such as - points, lines, polygons, and multipolygon. These queries use ST_DISTANCE, ST_WITHIN, ST_INTERSECTS keywords. The following are some examples that use spatial index kind:
94
119
95
-
- Geospatial distance queries:
120
+
- Geospatial distance queries:
96
121
97
122
```sql
98
123
SELECT * FROM container c WHERE ST_DISTANCE(c.property, { "type": "Point", "coordinates": [0.0, 10.0] }) < 40
99
124
```
100
125
101
-
- Geospatial within queries:
126
+
- Geospatial within queries:
102
127
103
128
```sql
104
129
SELECT * FROM container c WHERE ST_WITHIN(c.property, {"type": "Point", "coordinates": [0.0, 10.0] } })
105
130
```
106
131
132
+
- Geospatial intersect queries:
133
+
134
+
```sql
135
+
SELECT * FROM c WHERE ST_INTERSECTS(c.property, { 'type':'Polygon', 'coordinates': [[ [31.8, -5], [32, -5], [31.8, -5] ]] })
136
+
```
137
+
107
138
Spatial indexes can be used on correctly formatted [GeoJSON](geospatial.md) objects. Points, LineStrings, Polygons, and MultiPolygons are currently supported.
108
139
109
-
The **composite** index kind is used for:
140
+
### Composite indexes
141
+
142
+
**Composite** indices increase the efficiency when you are performing operations on multiple fields. The composite index kind is used for:
110
143
111
144
- `ORDER BY` queries on multiple properties:
112
145
@@ -126,6 +159,13 @@ The **composite** index kind is used for:
126
159
SELECT * FROM container c WHERE c.property1 = 'value' AND c.property2 > 'value'
127
160
```
128
161
162
+
As long as one filter predicate uses on of the index kind, the query engine will evaluate that first before scanning the rest. For example, if you have a SQL query such as `SELECT * FROM c WHERE c.firstName = "Andrew" and CONTAINS(c.lastName, "Liu")`
163
+
164
+
* The above query will first filter for entries where firstName = "Andrew" by using the index. It then pass all of the firstName = "Andrew" entries through a subsequent pipeline to evaluate the CONTAINS filter predicate.
165
+
166
+
* You can speed up queries and avoid full container scans when using functions that don’t use the index (e.g. CONTAINS) by adding additional filter predicates that do use the index. The order of filter clauses isn't important. The query engine is will figure out which predicates are more selective and run the query accordingly.
167
+
168
+
129
169
## Querying with indexes
130
170
131
171
The paths extracted when indexing data make it easy to lookup the index when processing a query. By matching the `WHERE` clause of a query with the list of indexed paths, it is possible to identify the items that match the query predicate very quickly.
0 commit comments