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
description: Learn how to query containers in Azure Cosmos DB using in-partition and cross-partition queries
2
+
title: Query an Azure Cosmos DB container
3
+
description: Learn how to query containers in Azure Cosmos DB by using both in-partition and cross-partition queries.
4
4
author: seesharprun
5
5
ms.service: cosmos-db
6
6
ms.subservice: nosql
7
7
ms.topic: how-to
8
-
ms.date: 3/18/2019
8
+
ms.date: 03/09/2023
9
9
ms.author: sidandrews
10
10
ms.reviewer: mjbrown
11
11
---
12
12
13
13
# Query an Azure Cosmos DB container
14
+
14
15
[!INCLUDE[NoSQL](../includes/appliesto-nosql.md)]
15
16
16
17
This article explains how to query a container (collection, graph, or table) in Azure Cosmos DB. In particular, it covers how in-partition and cross-partition queries work in Azure Cosmos DB.
@@ -19,13 +20,13 @@ This article explains how to query a container (collection, graph, or table) in
19
20
20
21
When you query data from containers, if the query has a partition key filter specified, Azure Cosmos DB automatically optimizes the query. It routes the query to the [physical partitions](../partitioning-overview.md#physical-partitions) corresponding to the partition key values specified in the filter.
21
22
22
-
For example, consider the below query with an equality filter on `DeviceId`. If we run this query on a container partitioned on `DeviceId`, this query will filter to a single physical partition.
23
+
For example, consider the below query with an equality filter on `DeviceId`. If we run this query on a container partitioned on `DeviceId`, this query filters to a single physical partition.
23
24
24
25
```sql
25
26
SELECT*FROM c WHEREc.DeviceId='XMS-0001'
26
27
```
27
28
28
-
As with the earlier example, this query will also filter to a single partition. Adding the additional filter on `Location`does not change this:
29
+
As with the earlier example, this query also filters to a single partition. Adding the filter on `Location`doesn't change the following query:
29
30
30
31
```sql
31
32
SELECT*FROM c WHEREc.DeviceId='XMS-0001'ANDc.Location='Seattle'
@@ -39,67 +40,68 @@ SELECT * FROM c WHERE c.DeviceId > 'XMS-0001'
39
40
40
41
## Cross-partition query
41
42
42
-
The following query doesn't have a filter on the partition key (`DeviceId`). Therefore, it must fan-out to all physical partitions where it is run against each partition's index:
43
+
The following query doesn't have a filter on the partition key (`DeviceId`). Therefore, it must fanout to all physical partitions where it's run against each partition's index:
43
44
44
45
```sql
45
46
SELECT*FROM c WHEREc.Location='Seattle`
46
47
```
47
48
48
-
Each physical partition has its own index. Therefore, when you run a cross-partition query on a container, you are effectively running one query *per* physical partition. Azure Cosmos DB will automatically aggregate results across different physical partitions.
49
+
Each physical partition has its own index. Therefore, when you run a cross-partition query on a container, you're effectively running one query *per* physical partition. Azure Cosmos DB automatically aggregates results across different physical partitions.
49
50
50
-
The indexes in different physical partitions are independent from one another. There is no global index in Azure Cosmos DB.
51
+
The indexes in different physical partitions are independent from one another. There's no global index in Azure Cosmos DB.
51
52
52
53
## Parallel cross-partition query
53
54
54
55
The Azure Cosmos DB SDKs 1.9.0 and later support parallel query execution options. Parallel cross-partition queries allow you to perform low latency, cross-partition queries.
55
56
56
57
You can manage parallel query execution by tuning the following parameters:
57
58
58
-
- **MaxConcurrency**: Sets the maximum number of simultaneous network connections to the container's partitions. If you set this property to `-1`, the SDK manages the degree of parallelism. If the `MaxConcurrency`set to `0`, there is a single network connection to the container's partitions.
59
+
- **MaxConcurrency**: Sets the maximum number of simultaneous network connections to the container's partitions. If you set this property to `-1`, the SDK manages the degree of parallelism. If the `MaxConcurrency`is set to `0`, there's a single network connection to the container's partitions.
59
60
60
61
-**MaxBufferedItemCount**: Trades query latency versus client-side memory utilization. If this option is omitted or to set to -1, the SDK manages the number of items buffered during parallel query execution.
61
62
62
-
Because of the Azure Cosmos DB's ability to parallelize cross-partition queries, query latency will generally scale well as the system adds [physical partitions](../partitioning-overview.md#physical-partitions). However, RU charge will increase significantly as the total number of physical partitions increases.
63
+
Because of the Azure Cosmos DB's ability to parallelize cross-partition queries, query latency generally scales well as the system adds [physical partitions](../partitioning-overview.md#physical-partitions). However, RU charge increases significantly as the total number of physical partitions increases.
63
64
64
-
When you run a cross-partition query, you are essentially doing a separate query per individual physical partition. While cross-partition queries will use the index, if available, they are still not nearly as efficient asin-partition queries.
65
+
When you run a cross-partition query, you're essentially doing a separate query per individual physical partition. While cross-partition queries use the index, if available, they still aren't nearly as efficient as in-partition queries.
65
66
66
67
## Useful example
67
68
68
-
Here's an analogy to better understand cross-partition queries:
69
+
Here's an analogy to better explain cross-partition queries:
69
70
70
-
Let's imagine you are a delivery driver that has to deliver packages to different apartment complexes. Each apartment complex has a list on the premises that has all of the resident's unit numbers. We can compare each apartment complex to a physical partition and each list to the physical partition's index.
71
+
Imagine you're a delivery driver that has to deliver packages to different apartment complexes. Each apartment complex has a list on the premises that has all of the residents' unit numbers. We can compare each apartment complex to a physical partition and each list to the physical partition's index.
71
72
72
73
We can compare in-partition and cross-partition queries using this example:
73
74
74
-
### In-partition query
75
+
### In-partition query (example)
75
76
76
-
If the delivery driver knows the correct apartment complex (physical partition), then they can immediately drive to the correct building. The driver can check the apartment complex's list of the resident's unit numbers (the index) and quickly deliver the appropriate packages. In this case, the driver does not waste any timeor effort driving to an apartment complex to checkand see if any package recipients live there.
77
+
If the delivery driver knows the correct apartment complex (physical partition), then they can immediately drive to the correct building. The driver can check the apartment complex's list of the residents' unit numbers (the index) and quickly deliver the appropriate packages. In this case, the driver doesn't waste any timeor effort driving to an apartment complex to checkand see if any package recipients live there.
77
78
78
79
### Cross-partition query (fan-out)
79
80
80
-
If the delivery driver does not know the correct apartment complex (physical partition), they'll need to drive to every single apartment building and check the list with all of the resident's unit numbers (the index). Once they arrive at each apartment complex, they'll still be able to use the list of the addresses of each resident. However, they will need to check every apartment complex's list, whether any package recipients live there or not. This is how cross-partition queries work. While they can use the index (don't need to knock on every single door), they must separately check the index for every physical partition.
81
+
If the delivery driver doesn't know the correct apartment complex (physical partition), they need to drive to every single apartment building and check the list with all of the residents' unit numbers (the index). Once the driver arrives at each apartment complex, they're still able to use the list of the addresses of each resident. However, they need to check every apartment complex's list, whether any package recipients live there or not. This example is how cross-partition queries work. While they can use the index (meaning, they don't need to knock on every door), they must separately check the index for every physical partition.
81
82
82
83
### Cross-partition query (scoped to only a few physical partitions)
83
84
84
-
If the delivery driver knows that all package recipients live within a certain few apartment complexes, they won't need to drive to every single one. While driving to a few apartment complexes will still require more work than visiting just a single building, the delivery driver still saves significant timeand effort. If a query has the partition key in its filter with the `IN` keyword, it will only check the relevant physical partition's indexes for data.
85
+
If the delivery driver knows that all package recipients live within a certain few apartment complexes, they don't need to drive to every single one. While driving to a few apartment complexes still requires more work than visiting just a single building, the delivery driver still saves significant timeand effort. If a query has the partition key in its filter with the `IN` keyword, it only checks the relevant physical partition's indexes for data.
86
+
87
+
## Avoid cross-partition queries
85
88
86
-
## Avoiding cross-partition queries
89
+
For most containers, having some cross-partition queries is inevitable, which is okay! Nearly all query operations are supported across partitions, both for logical partition keys and physical partitions. Azure Cosmos DB also has many optimizations in the query engine and client SDKs to parallelize query execution across physical partitions.
87
90
88
-
For most containers, it's inevitable that you will have some cross-partition queries. Having some cross-partition queries is ok! Nearly all query operations are supported across partitions (both logical partition keys and physical partitions). Azure Cosmos DB also has many optimizations in the query engine and client SDKs to parallelize query execution across physical partitions.
91
+
For most read-heavy scenarios, we recommend selecting the most common property in your query filters. You should also make sure your partition key adheres to other [partition key selection best practices](../partitioning-overview.md#choose-partitionkey).
89
92
90
-
For most read-heavy scenarios, we recommend simply selecting the most common property in your query filters. You should also make sure your partition key adheres to other [partition key selection best practices](../partitioning-overview.md#choose-partitionkey).
93
+
Avoiding cross-partition queries typically only matters with large containers. You're charged a minimum of about 2.5 RU's each time you check a physical partition's index for results even if no items in the physical partition match the query's filter. As such, if you've only one (or just a few) physical partitions, cross-partition queries don't consume significantly more RU's than in-partition queries.
91
94
92
-
Avoiding cross-partition queries typically only matters with large containers. You are charged a minimum of about 2.5RU's each time you check a physical partition's index for results, even if no items in the physical partition match the query's filter. As such, if you have only one (or just a few) physical partitions, cross-partition queries will not consume significantly more RU's than in-partition queries.
95
+
The number of physical partitions is tied to the amount of provisioned RU's. Each physical partition allows for up to 10,000 provisioned RU's and can store up to 50 GB of data. Azure Cosmos DB automatically manages physical partitions for you. The number of physical partitionsin your container is dependent on your provisioned throughput and consumed storage.
93
96
94
-
The number of physical partitions is tied to the amount of provisioned RU's. Each physical partition allows for up to 10,000 provisioned RU's and can store up to 50 GB of data. Azure Cosmos DB will automatically manage physical partitions for you. The number of physical partitions inyour container is dependent on your provisioned throughput and consumed storage.
97
+
You should try to avoid cross-partition queries if your workload meets the following criteria:
95
98
96
-
You should try to avoid cross-partition queries if your workload meets the criteria below:
97
99
- You plan to have over 30,000 RU's provisioned
98
100
- You plan to store over 100 GB of data
99
101
100
102
## Next steps
101
103
102
104
See the following articles to learn about partitioning in Azure Cosmos DB:
103
105
104
-
- [Partitioning in Azure Cosmos DB](../partitioning-overview.md)
105
-
- [Synthetic partition keys in Azure Cosmos DB](synthetic-partition-keys.md)
106
+
- [Partitioning and horizontal scaling in Azure Cosmos DB](../partitioning-overview.md)
107
+
- [Create a synthetic partition key in Azure Cosmos DB](synthetic-partition-keys.md)
0 commit comments