{"id":8000,"date":"2024-05-21T08:00:17","date_gmt":"2024-05-21T15:00:17","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cosmosdb\/?p=8000"},"modified":"2024-05-22T08:10:42","modified_gmt":"2024-05-22T15:10:42","slug":"general-availability-index-advisor-in-azure-cosmos-db-for-nosql","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cosmosdb\/general-availability-index-advisor-in-azure-cosmos-db-for-nosql\/","title":{"rendered":"General Availability: Index Metrics in Azure Cosmos DB for NoSQL"},"content":{"rendered":"<p>You can now use the Azure Cosmos DB index metrics to optimize query performance with Index Advisor! Indexing metrics show two important pieces of information:<\/p>\n<ul>\n<li>Utilized indexes &#8211; Which paths in the indexing policy were used in your query and utilized by the Azure Cosmos DB query engine<\/li>\n<li>Recommended indexes &#8211; Which indexes can improve your query speed and reduce RU cost if added to the indexing policy<\/li>\n<\/ul>\n<p><strong>Enabling indexing metrics:<\/strong><\/p>\n<p>You can enable indexing metrics for a query by setting the\u00a0<strong>PopulateIndexMetrics<\/strong>\u00a0property to\u00a0true. When not specified,\u00a0<strong>PopulateIndexMetrics<\/strong> defaults to false. The indexing metrics are supported in the .NET SDK (version 3.21.0 or later) and Java SDK (version 4.19.0 or later), and the Python SDK (version 4.6.0 or later)<\/p>\n<p><strong>Index impact score:<\/strong><\/p>\n<p>The index impact score is the likelihood that an indexed path, based on the query shape, has a significant impact on query performance. In other words, the index impact score is the probability that, without that specific indexed path, the query RU charge would have been substantially higher.<\/p>\n<p>There are two possible index impact scores:\u00a0<strong>high<\/strong>\u00a0and\u00a0<strong>low<\/strong>. If you have multiple potential indexed paths, we recommend focusing on indexed paths with a\u00a0<strong>high<\/strong>\u00a0impact score.<\/p>\n<p>The only criteria used in the index impact score is the query shape. For example, in the below query, the indexed path\u00a0\/age\/?\u00a0would be assigned a\u00a0<strong>high<\/strong>\u00a0index impact score:<\/p>\n<pre class=\"prettyprint language-sql\"><code class=\"language-sql\">SELECT * \r\nFROM c \r\nWHERE c.age &gt; 35<\/code><\/pre>\n<p>The actual impact depending on the nature of the data, so not all indexes with <strong>high <\/strong>index impact scores will improve query performance. In the above example, the impact of an \/age\/? indexed path would be larger if fewer items matched the c.age &gt; 30 filter expression.<\/p>\n<p><strong>Examples:<\/strong><\/p>\n<p>To show example indexing metrics, we\u2019ve created an example container with about 9 million items. Here\u2019s an example item:<\/p>\n<pre class=\"prettyprint language-json\"><code class=\"language-json\">{\r\n\t\"id\": \"ax92n6231\",\r\n\t\"name\": \"James Codella\",\r\n\t\"address\": \"1 Cosmos DB Avenue\",\r\n\t\"company\": \"Microsoft\",\r\n\t\"job\": \"Product Manager\",\r\n\t\"_ts\": 3141592653589\r\n}<\/code><\/pre>\n<p>The container has the default indexing policy of all paths indexed with a range index. We\u2019ll show the index metrics, both the utilized indexes and potential single indexes, for several different queries.<\/p>\n<p><strong>Query 1:<\/strong><\/p>\n<pre class=\"prettyprint language-sql\"><code class=\"language-sql\">SELECT * \r\nFROM c \r\nWHERE c.name = \u201cGeorge Washington\u201d<\/code><\/pre>\n<pre><strong>Original RU charge:<\/strong> 2.79 RUs<\/pre>\n<p><strong>Index metrics:<\/strong><\/p>\n<pre>Index Utilization Information\r\n  Utilized Single Indexes\r\n \u00a0\u00a0 Index Spec: \/name\/?\r\n \u00a0\u00a0 Index Impact Score: High\r\n \u00a0\u00a0 ---\r\n  Potential Single Indexes\r\n  Utilized Composite Indexes\r\n  Potential Composite Indexes<\/pre>\n<p><strong>Explanation<\/strong>:<\/p>\n<p>In this simple example, the indexing metrics show that the query used the existing \/name\/? indexed path. Given the <strong>high<\/strong> index impact score, there\u2019s a high likelihood that this indexed path was critical for query performance. There aren\u2019t any recommendations for further improving query performance.<\/p>\n<p><strong>Query 2: <\/strong><\/p>\n<pre class=\"prettyprint language-sql\"><code class=\"language-sql\">SELECT AVG(c._ts)\r\nFROM c\r\nWHERE c.job = \u201cDeveloper\u201d<\/code><\/pre>\n<p><strong>Original RU charge:<\/strong> 222.35 RUs<\/p>\n<p><strong>Index metrics:<\/strong><\/p>\n<pre>Index Utilization Information\r\n  Utilized Single Indexes\r\n \u00a0\u00a0 Index Spec: \/job\/?\r\n \u00a0\u00a0 Index Impact Score: High\r\n \u00a0\u00a0 ---\r\n \u00a0\u00a0 Index Spec: \/_ts\/?\r\n \u00a0\u00a0 Index Impact Score: High\r\n \u00a0\u00a0 ---\r\n  Potential Single Indexes\r\n  Utilized Composite Indexes\r\n  Potential Composite Indexes\r\n \u00a0\u00a0 Index Spec: \/job ASC, \/_ts ASC\r\n \u00a0\u00a0 Index Impact Score: High<\/pre>\n<p><strong>Explanation<\/strong>:<\/p>\n<p>This query averages the timestamp employees with the job \u201cDeveloper\u201d. While the query used the existing indexed paths \/job\/? and \/_ts\/?, the indexing metrics show a recommended composite index with a high likelihood of impact. In this case, after adding a composite index on (\/job ASC, \/_ts ASC), the RU charge of the query is reduced from 222.35 RUs to 137.56 RUs.<\/p>\n<p><strong>Query 3: <\/strong><\/p>\n<pre class=\"prettyprint language-default\"><code class=\"language-default\">SELECT TOP 10 *\r\nFROM c WHERE c.job = \u201cDeveloper\u201d\r\nAND c._ts &gt; 1589840249<\/code><\/pre>\n<p><strong>Original RU charge:<\/strong> 31.3 RUs<\/p>\n<p><strong>Index metrics:<\/strong><\/p>\n<pre>Index Utilization Information\r\n  Utilized Single Indexes\r\n \u00a0\u00a0 Index Spec: \/job\/?\r\n \u00a0\u00a0 Index Impact Score: High\r\n \u00a0\u00a0 ---\r\n \u00a0\u00a0 Index Spec: \/_ts\/?\r\n \u00a0\u00a0 Index Impact Score: High\r\n \u00a0\u00a0 ---\r\n  Potential Single Indexes\r\n  Utilized Composite Indexes\r\n  Potential Composite Indexes\r\n \u00a0\u00a0 Index Spec: \/job ASC, \/_ts ASC\r\n \u00a0\u00a0 Index Impact Score: High<\/pre>\n<p><strong>Explanation<\/strong>:<\/p>\n<p>This filters to all developers and sorts by timestamp . While the query used the existing indexed paths \/job\/? and \/_ts\/?, the indexing metrics show a recommended composite index with a high likelihood of impact. In this case, after adding a composite index on (\/job ASC, \/_ts ASC), the RU charge of the query is reduced from RUs 31.3 to 23.7 RUs.<\/p>\n<h3><strong>Next steps:<\/strong><\/h3>\n<ul>\n<li><a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/sql\/index-metrics\" target=\"_blank\" rel=\"noopener\">Learn more about using the indexing metrics to optimize high RU queries<\/a>.<\/li>\n<li><a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/sql\/troubleshoot-query-performance\" target=\"_blank\" rel=\"noopener\">Troubleshoot query performance<\/a><\/li>\n<li><a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/sql\/sql-query-getting-started\" target=\"_blank\" rel=\"noopener\">Getting started with SQL queries in Azure Cosmos DB<\/a><\/li>\n<\/ul>\n<p>Do you have any feedback about the indexing metrics? Share feedback directly with the Azure Cosmos DB engineering team: <a href=\"mailto:cosmosdbindexing@microsoft.com\">cosmosdbindexing [@] microsoft.com<\/a><\/p>\n<h2>About Azure Cosmos DB<\/h2>\n<p><span data-contrast=\"none\">Azure Cosmos DB is a fully managed and serverless distributed database for modern app development, with SLA-backed speed and availability, automatic and instant scalability, and support for open-source PostgreSQL, MongoDB and Apache Cassandra.\u00a0<\/span><a href=\"https:\/\/cosmos.azure.com\/try\/\" target=\"_blank\" rel=\"noopener\"><span data-contrast=\"none\">Try Azure Cosmos DB for free here<\/span><\/a><span data-contrast=\"none\">. To stay in the loop on Azure Cosmos DB updates, follow us on\u00a0<\/span><a href=\"https:\/\/twitter.com\/AzureCosmosDB\" target=\"_blank\" rel=\"noopener\"><span data-contrast=\"none\">X<\/span><\/a><span data-contrast=\"none\">,\u00a0<\/span><a href=\"https:\/\/aka.ms\/AzureCosmosDBYouTube\" target=\"_blank\" rel=\"noopener\"><span data-contrast=\"none\">YouTube<\/span><\/a><span data-contrast=\"none\">, and\u00a0<\/span><a href=\"https:\/\/www.linkedin.com\/company\/azure-cosmos-db\/\" target=\"_blank\" rel=\"noopener\"><span data-contrast=\"none\">LinkedIn<\/span><\/a><span data-contrast=\"none\">. To quickly build your first database, watch our\u00a0<\/span><a href=\"https:\/\/youtube.com\/playlist?list=PLmamF3YkHLoLLGUtSoxmUkORcWaTyHlXp\" target=\"_blank\" rel=\"noopener\"><span data-contrast=\"none\">Get Started videos<\/span><\/a><span data-contrast=\"none\">\u00a0on YouTube and explore ways to\u00a0<\/span><a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/optimize-dev-test\" target=\"_blank\" rel=\"noopener\"><span data-contrast=\"none\">dev\/test free.<\/span><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can now use the Azure Cosmos DB index metrics to optimize query performance with Index Advisor! Indexing metrics show two important pieces of information: Utilized indexes &#8211; Which paths in the indexing policy were used in your query and utilized by the Azure Cosmos DB query engine Recommended indexes &#8211; Which indexes can improve [&hellip;]<\/p>\n","protected":false},"author":118435,"featured_media":8109,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[14],"tags":[],"class_list":["post-8000","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-sql-api"],"acf":[],"blog_post_summary":"<p>You can now use the Azure Cosmos DB index metrics to optimize query performance with Index Advisor! Indexing metrics show two important pieces of information: Utilized indexes &#8211; Which paths in the indexing policy were used in your query and utilized by the Azure Cosmos DB query engine Recommended indexes &#8211; Which indexes can improve [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/8000","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\/118435"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/comments?post=8000"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/8000\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media\/8109"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media?parent=8000"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/categories?post=8000"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/tags?post=8000"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}