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/how-to-sql-subquery.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -55,7 +55,7 @@ WHERE t.name = 'infant formula' AND (n.nutritionValue > 0
55
55
ANDn.nutritionValue<10) ANDs.amount>1
56
56
```
57
57
58
-
For this query, the index will match any document that has a tag with the name "infant formula." It's a nutrient item with a value between 0 and 10 and a serving item with an 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.
58
+
For this query, the index will match any document that has a tag with the name "infant formula." It's a nutrient item with a value between 0 and 10, and a serving item with an 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.
59
59
60
60
The WHERE clause will then apply the filter predicate on each <c, t, n, s> tuple. 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.
61
61
@@ -73,9 +73,9 @@ Assume that only one item in the tags array matches the filter, and there are fi
73
73
74
74
### Evaluate once and reference many times
75
75
76
-
Subqueries can help optimize queries with expensive expressions such as user-defined functions (UDFs), or complex strings or arithmetic expressions. You can use a subquery along with a JOIN expression to evaluate the expression once but reference it many times.
76
+
Subqueries can help optimize queries with expensive expressions such as user-defined functions (UDFs), complex strings, or arithmetic expressions. You can use a subquery along with a JOIN expression to evaluate the expression once but reference it many times.
77
77
78
-
The following query runs the UDF GetMaxNutritionValue twice:
78
+
The following query runs the UDF `GetMaxNutritionValue` twice:
79
79
80
80
```sql
81
81
SELECTc.id, udf.GetMaxNutritionValue(c.nutrients) AS MaxNutritionValue
@@ -116,7 +116,7 @@ WHERE AvgNutritionValue > 80
116
116
117
117
### Mimic join with external reference data
118
118
119
-
You might often need to reference static data that rarely changes, such as units of measurements or country codes. It’s better not to duplicate such data for each document. Avoiding this duplication will save on storage and improve write performance by keeping the document size smaller. You can use a subquery to mimic inner-join semantics with a collection of reference data.
119
+
You might often need to reference static data that rarely changes, such as units of measurement or country codes. It’s better not to duplicate such data for each document. Avoiding this duplication will save on storage and improve write performance by keeping the document size smaller. You can use a subquery to mimic inner-join semantics with a collection of reference data.
120
120
121
121
For instance, consider this set of reference data:
122
122
@@ -289,7 +289,7 @@ Query output:
289
289
290
290
**Example 2**
291
291
292
-
A subquery with multiple aggregate function expressions:
292
+
Here's a subquery with multiple aggregate function expressions:
293
293
294
294
```sql
295
295
SELECT TOP 5f.id, (
@@ -395,7 +395,7 @@ WHERE n.units= "mg" AND n.nutritionValue > 0
395
395
396
396
For each of the documents in the collection, a cross-product is performed with its array elements. This JOIN operation makes it possible to filter on properties within the array. However, this query’s RU consumption will be significant. For instance, if 1,000 documents had 100 items in each array, it will expand to 1,000 x 100 (that is, 100,000) tuples.
397
397
398
-
Using `EXISTS` can help to avoid this expensive cross-product:
398
+
Using EXISTS can help to avoid this expensive cross-product:
399
399
400
400
```sql
401
401
SELECT VALUE c.description
@@ -407,7 +407,7 @@ WHERE EXISTS(
407
407
)
408
408
```
409
409
410
-
In this case, you filter on array elements within the EXISTS subquery. If an array element matches the filter, then you project it and `EXISTS` evaluates to true.
410
+
In this case, you filter on array elements within the EXISTS subquery. If an array element matches the filter, then you project it and EXISTS evaluates to true.
411
411
412
412
You can also alias EXISTS and reference it in the projection:
413
413
@@ -432,7 +432,7 @@ Query output:
432
432
433
433
#### ARRAY expression
434
434
435
-
You can use the `ARRAY` expression to project the results of a query as an array. You can use this expression only within the SELECT clause of the query.
435
+
You can use the ARRAY expression to project the results of a query as an array. You can use this expression only within the SELECT clause of the query.
436
436
437
437
```sql
438
438
SELECT TOP 1f.id, ARRAY(SELECT VALUE t.nameFROM t inf.tags) AS tagNames
@@ -455,7 +455,7 @@ Query output:
455
455
]
456
456
```
457
457
458
-
As with other subqueries, filters with the `ARRAY` expression are possible.
458
+
As with other subqueries, filters with the ARRAY expression are possible.
459
459
460
460
```sql
461
461
SELECT TOP 1c.id, ARRAY(SELECT VALUE t FROM t inc.tagsWHEREt.name!='infant formula') AS tagNames
0 commit comments