{"id":773,"date":"2020-04-09T10:08:34","date_gmt":"2020-04-09T17:08:34","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cosmosdb\/?p=773"},"modified":"2020-04-20T11:37:38","modified_gmt":"2020-04-20T18:37:38","slug":"april-query-improvements","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cosmosdb\/april-query-improvements\/","title":{"rendered":"April query updates in Azure Cosmos DB"},"content":{"rendered":"<p>We are constantly adding new query and indexing features to Azure Cosmos DB. This blog outlines a few recent query and indexing improvements for:<\/p>\n<ul>\n<li>Aggregates<\/li>\n<li>Inequality filters and filters on undefined values<\/li>\n<li>High precision dates and times<\/li>\n<\/ul>\n<p>In this post, we&#8217;ll use a sample 5 GB dataset to show the significant reduction in RU consumption for many queries that utilize these improvements. To highlight the impact of these improvements, we&#8217;ll compare query RU charges with the RU charges if you had run these queries in early 2019. In the dataset, documents have the following structure:<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-775\" src=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2020\/04\/sampleJSON-300x154.png\" alt=\"SampleJSONDocument\" width=\"1000\" height=\"515\" srcset=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2020\/04\/sampleJSON-300x154.png 300w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2020\/04\/sampleJSON.png 680w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" \/><\/p>\n<p>The arrivalTime property is defined for about 95% of the documents in this dataset. All other properties are defined for all documents.<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: 18pt;\"><strong>Aggregate performance improvements<\/strong><\/span><\/p>\n<p>\u00adWe are excited to announce improved index utilization for aggregate functions in Azure Cosmos DB. The RU charge and execution time for many aggregate functions have significantly improved.<\/p>\n<p>The following <a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/sql-query-aggregates\">aggregate system functions<\/a> will now benefit from an index. They\u2019ll consume fewer RU\u2019s and execute with lower latency than before:<\/p>\n<ul>\n<li>COUNT<\/li>\n<li>SUM<\/li>\n<li>AVG<\/li>\n<li>MAX<\/li>\n<li>MIN<\/li>\n<\/ul>\n<p>Here\u2019s an example query that finds the maximum airplane speed in our dataset where altitude is greater than 36,000:<\/p>\n<p><strong>Query:<\/strong><\/p>\n<pre>SELECT MAX(c.speed) FROM c WHERE c.altitude &gt; 36000<\/pre>\n<p><strong>Original RU charge:<\/strong> 11,201.54 RUs<\/p>\n<p><strong>Current RU charge (with index improvements):<\/strong> 121.69 RUs<\/p>\n<p>&nbsp;<\/p>\n<p>Here\u2019s another example that computes the count of documents with \u201cLAX\u201d as the departure airport:<\/p>\n<p><strong>Query:<\/strong><\/p>\n<pre>SELECT COUNT(1) FROM c WHERE c.departureAirport = \u201cLAX\u201d<\/pre>\n<p><strong>Original RU charge<\/strong>: 12,823.04 RUs<\/p>\n<p><strong>Current RU charge (with index improvements):<\/strong> 24.58 RUs<\/p>\n<p>&nbsp;<\/p>\n<p>To utilize these query engine improvements, you should <a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/index-policy#include-exclude-paths\">add an index<\/a> for properties in aggregate system functions.<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: 18pt;\"><strong>Performance improvements for inequality filters and filters on undefined values<\/strong><\/span><\/p>\n<p>Queries with inequality filters or filters on undefined values can now be run more efficiently. Previously, these filters did not utilize the index. When executing a query, Azure Cosmos DB would first evaluate other less expensive filters (such as =, &gt;, or &lt;) in the query. If there were inequality filters or filters on undefined values remaining, the query engine would be required to load each of these documents. Since inequality filters and filters on undefined values now utilize the index, we can avoid loading these documents and see a significant improvement in RU charge.<\/p>\n<p>Here\u2019s a full list of query filters with improvements:<\/p>\n<ul>\n<li>Inequality comparison expression (e.g. c.age != 4)<\/li>\n<li>NOT IN expression (e.g. c.name NOT IN (\u2018Luis\u2019, &#8216;Andrew&#8217;, &#8216;Deborah&#8217;))<\/li>\n<li>NOT IsDefined<\/li>\n<li>Is&lt;Type&gt; expressions (e.g. NOT IsDefined(c.age), NOT IsString(c.name))<\/li>\n<li><a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/sql-query-operators#-and--operators\">Coalesce<\/a> operator expression (e.g. (c.name ?? &#8216;N\/A&#8217;) = &#8216;Thomas&#8217;)<\/li>\n<li><a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/sql-query-operators#-and--operators\">Ternary<\/a> operator expression (e.g. c.name = null ? &#8216;N\/A&#8217; : c.name)<\/li>\n<\/ul>\n<p>If you have queries with these filters, you should <a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/index-policy#include-exclude-paths\">add an index<\/a> for the relevant properties.<\/p>\n<p>Here\u2019s an example query with an inequality filter:<\/p>\n<p><strong>Query:<\/strong><\/p>\n<pre>SELECT TOP 2000 * FROM c WHERE c.departureAirport = \"ORD\" AND c.altitude &lt; 10000 AND c.arrivalAirport NOT IN(\"JFK\", \"SEA\", \"EWR\", \"LHR\", \"LGA\",\"MCO\", \"SFO\",\"LAX\", \"DEN\", \"PHL\", \"SAN\")<\/pre>\n<p><strong>Original RU charge:<\/strong> 1,328.93 RUs<\/p>\n<p><strong>Current RU charge (with index improvements):<\/strong> 637.92 RUs<\/p>\n<p>&nbsp;<\/p>\n<p>Here\u2019s another example with a filter on an undefined value:<\/p>\n<p><strong>Query:<\/strong><\/p>\n<pre>SELECT TOP 10 * FROM c WHERE c. arrivalAirport = \u201cORD\u201d and NOT IS_DEFINED(c.arrivalTime)<\/pre>\n<p><strong>Original RU charge:<\/strong> 11,296.10 RUs<\/p>\n<p><strong>Current RU charge (with index improvements):<\/strong> 26.34 RUs<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: 18pt;\"><strong>High precision dates and times:<\/strong><\/span><\/p>\n<p>We have increased the precision from milliseconds to 100 nano seconds increments for the <strong>GetCurrentDateTime<\/strong> system function in Azure Cosmos DB. <strong>GetCurrentDateTime <\/strong>now returns the date in ISO 8601 format with seven-digit fractional second precision.<\/p>\n<p>Here is an example:<\/p>\n<p><strong>Query:<\/strong><\/p>\n<p>SELECT GetCurrentDateTime() AS currentUtcDateTime<\/p>\n<p><strong>Results: <\/strong><\/p>\n<pre>[{\r\n\"currentUtcDateTime\": \"2020-04-10T21:31:49.1234567Z\"\r\n}]\r\n\r\n<\/pre>\n<p><strong>Next steps:<\/strong><\/p>\n<ul>\n<li><a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/sql-query-getting-started\">Learn about queries in Azure Cosmos DB<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/AzureCosmosDB\/labs\/blob\/master\/dotnet\/labs\/03-querying_in_azure_cosmosdb.md\">Querying hands-on lab<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/AzureCosmosDB\/labs\/blob\/master\/dotnet\/labs\/04-indexing_in_cosmosdb.md\">Indexing hands-on lab<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>We are constantly adding new query and indexing features to Azure Cosmos DB. This blog outlines a few recent query and indexing improvements for aggregates, inequality filters, and<br \/>\ndates and time system functions. This blog covers updates during the first four months of 2020.<\/p>\n","protected":false},"author":12128,"featured_media":61,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[14,818,19],"tags":[],"class_list":["post-773","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-sql-api","category-query","category-tips-and-tricks"],"acf":[],"blog_post_summary":"<p>We are constantly adding new query and indexing features to Azure Cosmos DB. This blog outlines a few recent query and indexing improvements for aggregates, inequality filters, and<br \/>\ndates and time system functions. This blog covers updates during the first four months of 2020.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/773","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/users\/12128"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/comments?post=773"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/773\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media\/61"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media?parent=773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/categories?post=773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/tags?post=773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}