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/troubleshoot-query-performance.md
+21-21Lines changed: 21 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,7 @@ This document will use examples that can be recreated using the [nutrition](http
21
21
22
22
When optimizing a query in Azure Cosmos DB, the first step is always to [obtain the query metrics](profile-sql-api-query.md) for your query. These are also available through the Azure portal as shown below:
After obtaining query metrics, compare the Retrieved Document Count with the Output Document Count for your query. Use this comparison to identify the relevant sections to reference below.
27
27
@@ -33,37 +33,37 @@ You can reference the below section to understand the relevant query optimizatio
33
33
34
34
#### Retrieved Document Count is significantly greater than Output Document Count
35
35
36
-
a.[Ensure that the indexing policy includes necessary paths](#ensure-that-the-indexing-policy-includes-necessary-paths)
36
+
-[Ensure that the indexing policy includes necessary paths](#ensure-that-the-indexing-policy-includes-necessary-paths)
37
37
38
-
b.[Understand which system functions utilize the index](#understand-which-system-functions-utilize-the-index)
38
+
-[Understand which system functions utilize the index](#understand-which-system-functions-utilize-the-index)
39
39
40
-
c.[Optimize queries with both a filter and an ORDER BY clause](#optimize-queries-with-both-a-filter-and-an-order-by-clause)
40
+
-[Optimize queries with both a filter and an ORDER BY clause](#optimize-queries-with-both-a-filter-and-an-order-by-clause)
41
41
42
-
d.[Optimize queries that use DISTINCT](#optimize-queries-that-use-distinct)
42
+
-[Optimize queries that use DISTINCT](#optimize-queries-that-use-distinct)
43
43
44
-
e.[Optimize JOIN expressions by using a subquery](#optimize-join-expressions-by-using-a-subquery)
44
+
-[Optimize JOIN expressions by using a subquery](#optimize-join-expressions-by-using-a-subquery)
45
45
46
46
<br>
47
47
48
48
#### Retrieved Document Count is approximately equal to Output Document Count
## Optimizations for queries where Retrieved Document Count significantly exceeds Output Document Count:
69
69
@@ -109,9 +109,9 @@ Retrieved Document Count (60,951) is significantly greater than Output Document
109
109
110
110
## Ensure that the indexing policy includes necessary paths
111
111
112
-
Your indexing policy should cover any properties included in WHERE clauses, ORDER BY clauses, JOINs, and most System Functions. The path specified in the index policy should match (case-sensitive) the property in the JSON documents.
112
+
Your indexing policy should cover any properties included in `WHERE` clauses, `ORDER BY` clauses, `JOIN`, and most System Functions. The path specified in the index policy should match (case-sensitive) the property in the JSON documents.
113
113
114
-
If we run a simple query on the [nutrition](https://github.com/CosmosDB/labs/blob/master/dotnet/setup/NutritionData.json) dataset, we observe a much lower RU charge when the property in the WHERE clause is indexed.
114
+
If we run a simple query on the [nutrition](https://github.com/CosmosDB/labs/blob/master/dotnet/setup/NutritionData.json) dataset, we observe a much lower RU charge when the property in the `WHERE` clause is indexed.
115
115
116
116
### Original
117
117
@@ -187,7 +187,7 @@ Other parts of the query may still utilize the index despite the system function
187
187
188
188
## Optimize queries with both a filter and an ORDER BY clause
189
189
190
-
While queries with a filter and an ORDER BY clause will normally utilize a range index, they will be more efficient if they can be served from a composite index. In addition to modifying the indexing policy, you should add all properties in the composite index to the ORDER BY clause. This query modification will ensure that it utilizes the composite index. You can observe the impact by running a query on the [nutrition](https://github.com/CosmosDB/labs/blob/master/dotnet/setup/NutritionData.json) dataset.
190
+
While queries with a filter and an `ORDER BY` clause will normally utilize a range index, they will be more efficient if they can be served from a composite index. In addition to modifying the indexing policy, you should add all properties in the composite index to the `ORDER BY` clause. This query modification will ensure that it utilizes the composite index. You can observe the impact by running a query on the [nutrition](https://github.com/CosmosDB/labs/blob/master/dotnet/setup/NutritionData.json) dataset.
191
191
192
192
### Original
193
193
@@ -217,7 +217,7 @@ Indexing policy:
217
217
218
218
### Optimized
219
219
220
-
Updated query (includes both properties in the ORDER BY clause):
220
+
Updated query (includes both properties in the `ORDER BY` clause):
221
221
222
222
```sql
223
223
SELECT*FROM c
@@ -257,7 +257,7 @@ Updated indexing policy:
257
257
258
258
## Optimize queries that use DISTINCT
259
259
260
-
It will be more efficient to find the `DISTINCT` set of results if the duplicate results are consecutive. Adding an ORDER BY clause to the query and a composite index will ensure that duplicate results are consecutive. If you need to ORDER BY multiple properties, add a composite index. You can observe the impact by running a query on the [nutrition](https://github.com/CosmosDB/labs/blob/master/dotnet/setup/NutritionData.json) dataset.
260
+
It will be more efficient to find the `DISTINCT` set of results if the duplicate results are consecutive. Adding an `ORDER BY` clause to the query and a composite index will ensure that duplicate results are consecutive. If you need to `ORDER BY` multiple properties, add a composite index. You can observe the impact by running a query on the [nutrition](https://github.com/CosmosDB/labs/blob/master/dotnet/setup/NutritionData.json) dataset.
261
261
262
262
### Original
263
263
@@ -283,7 +283,7 @@ ORDER BY c.foodGroup
283
283
**RU Charge:** 3.38 RU's
284
284
285
285
## Optimize JOIN expressions by using a subquery
286
-
Multi-value subqueries can optimize `JOIN` expressions by pushing predicates after each select-many expression rather than after all cross-joins in the WHERE clause.
286
+
Multi-value subqueries can optimize `JOIN` expressions by pushing predicates after each select-many expression rather than after all cross-joins in the `WHERE` clause.
287
287
288
288
Consider the following query:
289
289
@@ -299,7 +299,7 @@ AND n.nutritionValue < 10) AND s.amount > 1
299
299
300
300
**RU Charge:** 167.62 RU's
301
301
302
-
For this query, the index will match any document that has a tag with the name "infant formula", nutritionValue greater than 0, and serving amount greater than 1. The JOIN expression here will perform the cross-product of all items of tags, nutrients, and servings arrays for each matching document before any filter is applied. The WHERE clause will then apply the filter predicate on each `<c, t, n, s>` tuple.
302
+
For this query, the index will match any document that has a tag with the name "infant formula", nutritionValue greater than 0, and serving amount greater than 1. The `JOIN` expression here will perform the cross-product of all items of tags, nutrients, and servings arrays for each matching document before any filter is applied. The `WHERE` clause will then apply the filter predicate on each `<c, t, n, s>` tuple.
303
303
304
304
For instance, if a matching document had 10 items in each of the three arrays, it will expand to 1 x 10 x 10 x 10 (that is, 1,000) tuples. Using subqueries here can help in filtering out joined array items before joining with the next expression.
305
305
@@ -315,7 +315,7 @@ JOIN (SELECT VALUE s FROM s IN c.servings WHERE s.amount > 1)
315
315
316
316
**RU Charge:** 22.17 RU's
317
317
318
-
Assume that only one item in the tags array matches the filter, and there are five items for both nutrients and servings arrays. The JOIN expressions will then expand to 1 x 1 x 5 x 5 = 25 items, as opposed to 1,000 items in the first query.
318
+
Assume that only one item in the tags array matches the filter, and there are five items for both nutrients and servings arrays. The `JOIN` expressions will then expand to 1 x 1 x 5 x 5 = 25 items, as opposed to 1,000 items in the first query.
319
319
320
320
## Optimizations for queries where Retrieved Document Count is approximately equal to Output Document Count:
0 commit comments