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
# Get SQL query execution metrics and analyze query performance using .NET SDK
13
13
[!INCLUDE[NoSQL](../includes/appliesto-nosql.md)]
14
14
15
-
This article presents how to profile SQL query performance on Azure Cosmos DB. This profiling can be done using `QueryMetrics` retrieved from the .NET SDK and is detailed here. [QueryMetrics](/dotnet/api/microsoft.azure.documents.querymetrics)is a strongly typed object with information about the backend query execution. These metrics are documented in more detail in the [Tune Query Performance](./query-metrics.md) article.
15
+
This article presents how to profile SQL query performance on Azure Cosmos DBusing [ServerSideCumulativeMetrics](/dotnet/api/microsoft.azure.cosmos.serversidecumulativemetrics) retrieved from the .NET SDK. `ServerSideCumulativeMetrics`is a strongly typed object with information about the backend query execution. It contains cumulative metrics that are aggregated across all physical partitions for the request, and a list of metrics for each physical partition. These metrics are documented in more detail in the [Tune Query Performance](./query-metrics.md#query-execution-metrics) article.
16
16
17
-
## Set the FeedOptions parameter
17
+
## Get query metrics
18
18
19
-
All the overloads for [DocumentClient.CreateDocumentQuery](/dotnet/api/microsoft.azure.documents.client.documentclient.createdocumentquery) take in an optional [FeedOptions](/dotnet/api/microsoft.azure.documents.client.feedoptions) parameter. This option is what allows query execution to be tuned and parameterized.
20
-
21
-
To collect the NoSQL query execution metrics, you must set the parameter [PopulateQueryMetrics](/dotnet/api/microsoft.azure.documents.client.feedoptions.populatequerymetrics#P:Microsoft.Azure.Documents.Client.FeedOptions.PopulateQueryMetrics) in the [FeedOptions](/dotnet/api/microsoft.azure.documents.client.feedoptions) to `true`. Setting `PopulateQueryMetrics` to true will make it so that the `FeedResponse` will contain the relevant `QueryMetrics`.
22
-
23
-
## Get query metrics with AsDocumentQuery()
24
-
The following code sample shows how to do retrieve metrics when using [AsDocumentQuery()](/dotnet/api/microsoft.azure.documents.linq.documentqueryable.asdocumentquery) method:
19
+
Query metrics are available as a strongly typed object in the .NET SDK beginning in [version 3.36.0](https://www.nuget.org/packages/Microsoft.Azure.Cosmos/3.36.0). Prior to this version, or if you're using a different SDK language, you can retrieve query metrics by parsing the `Diagnostics`. The following code sample shows how to retrieve `ServerSideCumulativeMetrics` from the `Diagnostics` in a [FeedResponse](/dotnet/api/microsoft.azure.cosmos.feedresponse-1):
In the previous section, notice that there were multiple calls to [ExecuteNextAsync](/dotnet/api/microsoft.azure.documents.linq.idocumentquery-1.executenextasync) method. Each call returned a `FeedResponse`object that has a dictionary of `QueryMetrics`; one for every continuation of the query. The following example shows how to aggregate these `QueryMetrics` using LINQ:
38
+
You can also get query metrics from the `FeedResponse`of a LINQ query using the `ToFeedIterator()` method:
You can group the `QueryMetrics` by the Partition ID. Grouping by Partition ID allows you to see if a specific Partition is causing performance issues when compared to others. The following example shows how to group `QueryMetrics` with LINQ:
54
+
`ServerSideCumulativeMetrics` contains a `CumulativeMetrics` property that represents the query metrics aggregated over all partitions for the single round trip.
You can also aggregate these metrics across all round trips for the query. The following is an example of how to aggregate query execution time across all round trips for a given query using LINQ:
100
65
101
-
// If we wanted to we could even aggregate the groupedby QueryMetrics
You can also get the `FeedResponse` from a LINQ Query using the `AsDocumentQuery()` method:
88
+
`ServerSideCumulativeMetrics` contains a `PartitionedMetrics` property that is a list of per-partition metrics for the round trip. If multiple physical partitions are reached in a single round trip, then metrics for each of them appear in the list. Partitioned metrics are represented as [ServerSidePartitionedMetrics](/dotnet/api/microsoft.azure.cosmos.serversidepartitionedmetrics) with a unique identifier for each physical partition.
When accumulated over all round trips, per partition metrics allow you to see if a specific partition is causing performance issues when compared to others. The following is an example of how to group partition metrics for each trip using LINQ:
99
+
100
+
```csharp
101
+
QueryDefinitionquery=newQueryDefinition("SELECT TOP 5 * FROM c");
You can capture the request units consumed by each query to investigate expensive queries or queries that consume high throughput. You can get the request charge by using the [RequestCharge](/dotnet/api/microsoft.azure.documents.client.feedresponse-1.requestcharge) property in `FeedResponse`. To learn more about how to get the request charge using the Azure portal and different SDKs, see [find the request unit charge](find-request-unit-charge.md) article.
127
+
You can capture the request units consumed by each query to investigate expensive queries or queries that consume high throughput. You can get the request charge by using the `RequestCharge` property in `FeedResponse`. To learn more about how to get the request charge using the Azure portal and different SDKs, see [find the request unit charge](find-request-unit-charge.md) article.
When calculating the time required to execute a client-side query, make sure that you only include the time to call the `ExecuteNextAsync` method and not other parts of your code base. Just these calls help you in calculating how long the query execution took as shown in the following example:
146
+
You can capture query execution time for each trip from the query metrics. When looking at request latency, it's important to differentiate query execution time from other sources of latency, such as network transit time. The following example shows how to get cumulative query execution time for each round trip:
A scan query refers to a query that wasn't served by the index, due to which, many documents are loaded before returning the result set.
167
+
Looking at the index utilization can help you debug slow queries. Queries that can't use the index result in a full scan of all documents in a container before returning the result set.
168
168
169
-
Below is an example of a scan query:
169
+
Here's an example of a scan query:
170
170
171
171
```sql
172
172
SELECT VALUE c.description
@@ -185,21 +185,11 @@ Output Document Count : 7
185
185
Output Document Size : 510 bytes
186
186
Index Utilization : 0.00 %
187
187
Total Query Execution Time : 4,500.34 milliseconds
188
-
Query Preparation Times
189
-
Query Compilation Time : 0.09 milliseconds
190
-
Logical Plan Build Time : 0.05 milliseconds
191
-
Physical Plan Build Time : 0.04 milliseconds
192
-
Query Optimization Time : 0.01 milliseconds
193
-
Index Lookup Time : 0.01 milliseconds
194
-
Document Load Time : 4,177.66 milliseconds
195
-
Runtime Execution Times
196
-
Query Engine Times : 322.16 milliseconds
197
-
System Function Execution Time : 85.74 milliseconds
198
-
User-defined Function Execution Time : 0.00 milliseconds
199
-
Document Write Time : 0.01 milliseconds
200
-
Client Side Metrics
201
-
Retry Count : 0
202
-
Request Charge : 4,059.95 RUs
188
+
Query Preparation Time : 0.2 milliseconds
189
+
Index Lookup Time : 0.01 milliseconds
190
+
Document Load Time : 4,177.66 milliseconds
191
+
Runtime Execution Time : 407.9 milliseconds
192
+
Document Write Time : 0.01 milliseconds
203
193
```
204
194
205
195
Note the following values from the query metrics output:
@@ -225,7 +215,7 @@ FROM c
225
215
WHEREc.description="BABYFOOD, DESSERT, FRUIT DESSERT, WITHOUT ASCORBIC ACID, JUNIOR"
226
216
```
227
217
228
-
This query is now able to be served from the index.
218
+
This query is now able to be served from the index. Alternatively, you can use [computed properties](query/computed-properties.md) to index the results of system functions or complex calculations that would otherwise result in a full scan.
229
219
230
220
To learn more about tuning query performance, see the [Tune Query Performance](./query-metrics.md) article.
0 commit comments