Skip to content

Commit 0e35026

Browse files
Merge pull request #91532 from SnehaGunda/LandingPage2
Adding examples for index kinds
2 parents 2af3354 + cdd2183 commit 0e35026

File tree

1 file changed

+48
-8
lines changed

1 file changed

+48
-8
lines changed

articles/cosmos-db/index-overview.md

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Understand how indexing works in Azure Cosmos DB.
44
author: ThomasWeiss
55
ms.service: cosmos-db
66
ms.topic: conceptual
7-
ms.date: 09/10/2019
7+
ms.date: 10/11/2019
88
ms.author: thweiss
99
---
1010

@@ -59,26 +59,49 @@ When an item is written, Azure Cosmos DB effectively indexes each property's pat
5959

6060
## Index kinds
6161

62-
Azure Cosmos DB currently supports three kinds of indexes:
62+
Azure Cosmos DB currently supports three kinds of indexes.
6363

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:
6567

6668
- Equality queries:
6769

6870
```sql
6971
SELECT * FROM container c WHERE c.property = 'value'
7072
```
7173

74+
```sql
75+
SELECT * FROM c WHERE c.property IN ("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+
7283
- Range queries:
7384
7485
```sql
7586
SELECT * FROM container c WHERE c.property > 'value'
7687
```
7788
(works for `>`, `<`, `>=`, `<=`, `!=`)
7889
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+
79102
- `ORDER BY` queries:
80103
81-
```sql
104+
```sql
82105
SELECT * FROM container c ORDER BY c.property
83106
```
84107
@@ -90,23 +113,33 @@ The **range** index kind is used for:
90113
91114
Range indexes can be used on scalar values (string or number).
92115
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:
94119
95-
- Geospatial distance queries:
120+
- Geospatial distance queries:
96121
97122
```sql
98123
SELECT * FROM container c WHERE ST_DISTANCE(c.property, { "type": "Point", "coordinates": [0.0, 10.0] }) < 40
99124
```
100125
101-
- Geospatial within queries:
126+
- Geospatial within queries:
102127
103128
```sql
104129
SELECT * FROM container c WHERE ST_WITHIN(c.property, {"type": "Point", "coordinates": [0.0, 10.0] } })
105130
```
106131
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+
107138
Spatial indexes can be used on correctly formatted [GeoJSON](geospatial.md) objects. Points, LineStrings, Polygons, and MultiPolygons are currently supported.
108139
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:
110143
111144
- `ORDER BY` queries on multiple properties:
112145
@@ -126,6 +159,13 @@ The **composite** index kind is used for:
126159
SELECT * FROM container c WHERE c.property1 = 'value' AND c.property2 > 'value'
127160
```
128161
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+
129169
## Querying with indexes
130170
131171
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

Comments
 (0)