Skip to content

Commit 95b063f

Browse files
authored
Merge pull request #112802 from jcodella/patch-3
Update performance-tips-query-sdk.md with ODE
2 parents 4b15e9c + 1130146 commit 95b063f

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

articles/cosmos-db/nosql/performance-tips-query-sdk.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,49 @@ CosmosPagedFlux<MyItem> filteredItems =
359359

360360
Pre-fetching works the same way regardless of the degree of parallelism, and there's a single buffer for the data from all partitions.
361361

362+
## Optimizing single partition queries with Optimistic Direct Execution
363+
364+
Azure Cosmos DB NoSQL has an optimization called Optimistic Direct Execution (ODE), which can improve the efficiency of certain NoSQL queries. Specifically, queries that don’t require distribution include those that can be executed on a single physical partition or that have responses that don't require [pagination](query/pagination.md). Queries that don’t require distribution can confidently skip some processes, such as client-side query plan generation and query rewrite, thereby reducing query latency and RU cost. If you specify the partition key in the request or query itself (or have only one physical partition), and the results of your query don’t require pagination, then ODE can improve your queries.
365+
366+
Single partition queries that feature GROUP BY, ORDER BY, DISTINCT, and aggregation functions (like sum, mean, min, and max) can significantly benefit from using ODE. However, in scenarios where the query is targeting multiple partitions or still requires pagination, the latency of the query response and RU cost might be higher than without using ODE. Therefore, when using ODE, we recommend to:
367+
- Specify the partition key in the call or query itself.
368+
- Ensure that your data size hasn’t grown and caused the partition to split.
369+
- Ensure that your query results don’t require pagination to get the full benefit of ODE.
370+
371+
Here are a few examples of simple single partition queries which can benefit from ODE:
372+
```
373+
- SELECT * FROM r
374+
- SELECT VALUE r.id FROM r
375+
- SELECT * FROM r WHERE r.id > 5
376+
- SELECT r.id FROM r JOIN id IN r.id
377+
- SELECT TOP 5 r.id FROM r ORDER BY r.id
378+
- SELECT * FROM r WHERE r.id > 5 OFFSET 5 LIMIT 3
379+
```
380+
There can be cases where single partition queries may still require distribution if the number of data items increases over time and your Azure Cosmos DB database [splits the partition](../partitioning-overview.md#physical-partitions). Examples of queries where this could occur include:
381+
```
382+
- SELECT Count(r.id) AS count_a FROM r
383+
- SELECT DISTINCT r.id FROM r
384+
- SELECT Max(r.a) as min_a FROM r
385+
- SELECT Avg(r.a) as min_a FROM r
386+
- SELECT Sum(r.a) as sum_a FROM r WHERE r.a > 0
387+
```
388+
Some complex queries can always require distribution, even if targeting a single partition. Examples of such queries include:
389+
```
390+
- SELECT Sum(id) as sum_id FROM r JOIN id IN r.id
391+
- SELECT DISTINCT r.id FROM r GROUP BY r.id
392+
- SELECT DISTINCT r.id, Sum(r.id) as sum_a FROM r GROUP BY r.id
393+
- SELECT Count(1) FROM (SELECT DISTINCT r.id FROM root r)
394+
- SELECT Avg(1) AS avg FROM root r
395+
```
396+
397+
It's important to note that ODE might not always retrieve the query plan and, as a result, is not able to disallow or turn off for unsupported queries. For example, after partition split, such queries are no longer eligible for ODE and, therefore, won't run because client-side query plan evaluation will block those. To ensure compatibility/service continuity, it's critical to ensure that only queries that are fully supported in scenarios without ODE (that is, they execute and produce the correct result in the general multi-partition case) are used with ODE.
398+
399+
### Using ODE via the SDKs
400+
ODE is now available and enabled by default in the C# Preview SDK for versions 3.35.0 and later. When you execute a query and specify a partition key in the request or query itself, or your database has only one physical partition, your query execution can leverage the benefits of ODE.
401+
402+
To disable ODE, set the flag `EnableOptimisticDirectExecution` to false in your QueryRequestOptions object.
403+
404+
362405
## Next steps
363406

364407
To learn more about performance using the Java SDK:

0 commit comments

Comments
 (0)