{"id":10247,"date":"2025-05-19T09:00:22","date_gmt":"2025-05-19T16:00:22","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cosmosdb\/?p=10247"},"modified":"2025-05-17T15:17:33","modified_gmt":"2025-05-17T22:17:33","slug":"boost-query-performance-with-global-secondary-indexes-in-azure-cosmos-db","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cosmosdb\/boost-query-performance-with-global-secondary-indexes-in-azure-cosmos-db\/","title":{"rendered":"Boost Query Performance with Global Secondary Indexes in Azure Cosmos DB"},"content":{"rendered":"<p><span data-contrast=\"auto\"> Global secondary indexes for Azure Cosmos DB\u2014now in Public Preview\u2014make it easier to query data efficiently, especially as your datasets grow.<\/span><span data-ccp-props=\"{}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Distributed databases like\u202fAzure Cosmos DB scale with dataset growth by partitioning data across multiple physical machines. When your queries include the\u202fpartition key, the client routes them directly to the appropriate partition. Queries that include the partition key remain low-latency regardless of how many partitions exist.<\/span><\/p>\n<p><span data-contrast=\"auto\">But in real-world applications, or in the case of AI apps and agents, you often need to query data without including the partition key. That\u2019s where global secondary indexes (GSIs) help. GSIs let you define an alternate partition key and data model, allowing for fast, efficient lookups across a wider range of query patterns.<\/span><span data-ccp-props=\"{}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<h3><span class=\"TextRun SCXW94130752 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW94130752 BCX8\" data-ccp-parastyle=\"heading 2\">When to use GSI<\/span><\/span><\/h3>\n<p><span data-contrast=\"auto\">Adding global secondary indexes (GSIs) can lower costs and boost performance for many workloads. Azure Cosmos DB automatically syncs changes to data from the source container to the GSI container. Data is kept up to date with no extra work on your part. Because GSIs have their own container properties\u2014such as partition key, data model, and indexing policy\u2014you can optimize for diverse query patterns while avoiding cross-partition queries on the source container.<\/span><span data-ccp-props=\"{&quot;335559738&quot;:240,&quot;335559739&quot;:240}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">You can also add GSIs to containers as your application evolves, keeping queries efficient over time. Since GSIs exist as separate containers, they let you isolate specific parts of your workload. For example, you can create vector or full-text search indexes in the GSI to support vector and hybrid search, all without affecting transactional operations on the source container.<\/span><span data-ccp-props=\"{&quot;335559738&quot;:240,&quot;335559739&quot;:240}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<h3><span class=\"TextRun SCXW207776502 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW207776502 BCX8\" data-ccp-parastyle=\"heading 2\">Let\u2019s<\/span><span class=\"NormalTextRun SCXW207776502 BCX8\" data-ccp-parastyle=\"heading 2\"> see a real<\/span><span class=\"NormalTextRun SCXW207776502 BCX8\" data-ccp-parastyle=\"heading 2\">&#8211;<\/span><span class=\"NormalTextRun SCXW207776502 BCX8\" data-ccp-parastyle=\"heading 2\">world example<\/span><\/span><span class=\"EOP Selected SCXW207776502 BCX8\" data-ccp-props=\"{&quot;134245418&quot;:true,&quot;134245529&quot;:true,&quot;335559738&quot;:160,&quot;335559739&quot;:80}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"auto\">Contoso, a fictitious company, stores user information in an Azure Cosmos DB container. Because their verification process often involves looking up users by email, they chose to partition the container using the email address. However, some users prefer verification by phone number, so Contoso also needs to support lookups by that field.<\/span><span data-ccp-props=\"{&quot;335559738&quot;:240,&quot;335559739&quot;:240}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">The <strong>users<\/strong> container spans 10 physical partitions and currently holds 200,000 user records. Queries by email perform efficiently thanks to the partitioning strategy, but queries by phone number are less optimized and need to search across all 10 partitions. To improve performance and reduce query cost, Contoso added a global secondary index (GSI) called <\/span><b><span data-contrast=\"auto\">usersByPhone<\/span><\/b>.<span data-contrast=\"auto\"> They partitioned the GSI by phone number and used it to compare performance.<\/span><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"prettyprint language-sql\"><code class=\"language-sql\">SELECT *  \r\nFROM c  \r\nWHERE c.phoneNumber = \"555-123-4567\" <\/code><\/pre>\n<p><em>RU charge on users container<\/em>: 27.93\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <em>Execution time<\/em>: 6.90 seconds<\/p>\n<p><em>RU charge on GSI container<\/em>: 2.82\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <em>Execution time<\/em>: 0.49 seconds<\/p>\n<p>&nbsp;<\/p>\n<p><span class=\"TextRun SCXW109872804 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW109872804 BCX8\">In this scenario, executing the same query on the GSI container reduces both RU charges and execution time by <\/span><\/span><strong><span class=\"TextRun SCXW109872804 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW109872804 BCX8\">90%<\/span><\/span><\/strong><span class=\"TextRun SCXW109872804 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW109872804 BCX8\">.<\/span> <span class=\"NormalTextRun SCXW109872804 BCX8\">Since Contoso performs thousands of phone number lookups per second during peak load, the GSI delivers significant cost savings and improved performance.<\/span> <span class=\"NormalTextRun SCXW109872804 BCX8\">This improvement is driven by the client routing the query directly to the correct GSI partition, as shown below.<\/span><\/span><span class=\"EOP Selected SCXW109872804 BCX8\" data-ccp-props=\"{}\">\u00a0<\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2025\/05\/GSI_FindByPhone-1.png\"><img decoding=\"async\" class=\"wp-image-10258 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2025\/05\/GSI_FindByPhone-1-300x217.png\" alt=\"GSI FindByPhone image\" width=\"445\" height=\"322\" srcset=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2025\/05\/GSI_FindByPhone-1-300x217.png 300w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2025\/05\/GSI_FindByPhone-1-768x556.png 768w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2025\/05\/GSI_FindByPhone-1.png 994w\" sizes=\"(max-width: 445px) 100vw, 445px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Because GSIs are implemented as independent containers, you can define a custom indexing policy to further optimize queries. For example, Contoso runs targeted promotions for active users in specific area codes. To support this, they query their GSI and have added a composite index on <strong>isActive<\/strong> and <strong>phoneNumber<\/strong> to accelerate those lookups.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"prettyprint language-sql\"><code class=\"language-sql\">SELECT * \r\nFROM c \r\nWHERE STARTSWITH(c.phoneNumber, \"555\") AND c.isActive = true<\/code><\/pre>\n<p><em>RU charge on users container<\/em>: 33.32\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <em>Execution time<\/em>: 1.82 seconds<\/p>\n<p><em>RU charge on GSI container<\/em>: 6.98\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <em>Execution time<\/em>: 0.53 seconds<\/p>\n<p>&nbsp;<\/p>\n<p>For this query, Contoso sees an 80% reduction in RU charges and a 70% improvement in execution time. The exact savings depend on the query\u2019s complexity and the container\u2019s configuration. By leveraging Azure Cosmos DB\u2019s rich query syntax and flexible indexing capabilities, GSIs offer a powerful way to optimize performance.<\/p>\n<p>If you\u2019d like to try this sample yourself, <a href=\"https:\/\/github.com\/jcocchi\/cosmos-gsi-demo\">the code is available on GitHub<\/a>. The repository includes setup instructions, a data loader project, and a console app to run the queries and see the performance impact firsthand.<\/p>\n<p>&nbsp;<\/p>\n<h3>Getting started with global secondary indexes<\/h3>\n<p>You can enable global secondary indexes on your Azure Cosmos DB for NoSQL account directly from the Azure Portal. We\u2019ve also added support for accounts that use continuous backup\u2014this backup mode is now required before you can enable GSIs on your account.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2025\/05\/enable-global-secondary-indexes.png\"><img decoding=\"async\" class=\"wp-image-10255 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2025\/05\/enable-global-secondary-indexes-300x133.png\" alt=\"enable global secondary indexes image\" width=\"701\" height=\"311\" srcset=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2025\/05\/enable-global-secondary-indexes-300x133.png 300w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2025\/05\/enable-global-secondary-indexes-1024x454.png 1024w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2025\/05\/enable-global-secondary-indexes-768x341.png 768w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2025\/05\/enable-global-secondary-indexes.png 1280w\" sizes=\"(max-width: 701px) 100vw, 701px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Global secondary indexes (GSIs) are the evolution of the former materialized views feature. We\u2019ve made them easier and more cost-effective by removing the need to provision, manage, or pay for the builder component that handles data synchronization. Once you enable GSIs on your account, you can immediately start creating GSI containers\u2014either through the Azure Portal or using the Azure CLI. GSI containers should be created using autoscale throughput to account for spikes and dips in traffic without falling too far behind the source container. After creation, the GSI automatically begins syncing data from the source container.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2025\/05\/create-global-secondary-indexes.png\"><img decoding=\"async\" class=\"wp-image-10254 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2025\/05\/create-global-secondary-indexes-300x164.png\" alt=\"create global secondary indexes image\" width=\"675\" height=\"369\" srcset=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2025\/05\/create-global-secondary-indexes-300x164.png 300w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2025\/05\/create-global-secondary-indexes-1024x558.png 1024w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2025\/05\/create-global-secondary-indexes-768x419.png 768w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2025\/05\/create-global-secondary-indexes.png 1280w\" sizes=\"(max-width: 675px) 100vw, 675px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Global secondary indexes offer a powerful and flexible way to optimize query performance in Azure Cosmos DB for NoSQL. By supporting alternate partition keys and custom indexing policies, GSIs help you adapt to evolving application needs without compromising performance or scalability. Whether you&#8217;re improving lookup efficiency, isolating workloads, or enabling new query patterns, GSIs can help reduce cost and complexity.<\/p>\n<p>&nbsp;<\/p>\n<h3>Learn more<\/h3>\n<p>Explore the following resources to help you get started and make the most of GSIs in your applications:<\/p>\n<ul>\n<li>\ud83d\udcd8 <a href=\"https:\/\/learn.microsoft.com\/azure\/cosmos-db\/nosql\/global-secondary-indexes\">Global Secondary Indexes (Preview) \u2013 Azure Cosmos DB for NoSQL<\/a><\/li>\n<li>\u2699\ufe0f <a href=\"https:\/\/learn.microsoft.com\/azure\/cosmos-db\/nosql\/how-to-configure-global-secondary-indexes?tabs=azure-portal%2Cdotnet\">How to Configure Global Secondary Indexes (Preview)<\/a><\/li>\n<\/ul>\n<h2><strong>Leave a review<\/strong><\/h2>\n<p>Tell us about your Azure Cosmos DB experience! Leave a review on PeerSpot and we\u2019ll gift you $50.\u00a0<a id=\"menuros8\" class=\"fui-Link ___1q1shib f2hkw1w f3rmtva f1ewtqcl fyind8e f1k6fduh f1w7gpdv fk6fouc fjoy568 figsok6 f1s184ao f1mk8lai fnbmjn9 f1o700av f13mvf36 f1cmlufx f9n3di6 f1ids18y f1tx3yz7 f1deo86v f1eh06m1 f1iescvh fhgqx19 f1olyrje f1p93eir f1nev41a f1h8hb77 f1lqvz6u f10aw75t fsle3fq f17ae5zn\" title=\"https:\/\/peerspotdotcom.my.site.com\/proreviews\/?salesopportunityproduct=00kpy000004tkxjia4&amp;productpeerspotnumber=30881&amp;calendlyaccount=peerspot&amp;calendlyformlink=peerspot-product-reviews-ps-gc-vi-sf-50&amp;giftcard=50\" href=\"https:\/\/peerspotdotcom.my.site.com\/proReviews\/?SalesOpportunityProduct=00kPy000004TKXJIA4&amp;productPeerspotNumber=30881&amp;CalendlyAccount=peerspot&amp;CalendlyFormLink=peerspot-product-reviews-ps-gc-vi-sf-50&amp;giftCard=50\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Link Get started here\">Get started here<\/a>.<\/p>\n<h2><strong>About Azure Cosmos DB<\/strong><\/h2>\n<p>Azure Cosmos DB is a fully managed and serverless NoSQL and vector database for modern app development, including AI applications. With its SLA-backed speed and availability as well as instant dynamic scalability, it is ideal for real-time NoSQL and MongoDB applications that require high performance and distributed computing over massive volumes of NoSQL and vector data.<\/p>\n<p>To stay in the loop on Azure Cosmos DB updates, follow us on\u00a0<a id=\"menurosb\" class=\"fui-Link ___1q1shib f2hkw1w f3rmtva f1ewtqcl fyind8e f1k6fduh f1w7gpdv fk6fouc fjoy568 figsok6 f1s184ao f1mk8lai fnbmjn9 f1o700av f13mvf36 f1cmlufx f9n3di6 f1ids18y f1tx3yz7 f1deo86v f1eh06m1 f1iescvh fhgqx19 f1olyrje f1p93eir f1nev41a f1h8hb77 f1lqvz6u f10aw75t fsle3fq f17ae5zn\" title=\"https:\/\/twitter.com\/azurecosmosdb\" href=\"https:\/\/twitter.com\/AzureCosmosDB\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Link X\">X<\/a>,\u00a0<a id=\"menurose\" class=\"fui-Link ___1q1shib f2hkw1w f3rmtva f1ewtqcl fyind8e f1k6fduh f1w7gpdv fk6fouc fjoy568 figsok6 f1s184ao f1mk8lai fnbmjn9 f1o700av f13mvf36 f1cmlufx f9n3di6 f1ids18y f1tx3yz7 f1deo86v f1eh06m1 f1iescvh fhgqx19 f1olyrje f1p93eir f1nev41a f1h8hb77 f1lqvz6u f10aw75t fsle3fq f17ae5zn\" title=\"https:\/\/aka.ms\/azurecosmosdbyoutube\" href=\"https:\/\/aka.ms\/AzureCosmosDBYouTube\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Link YouTube\">YouTube<\/a>, and\u00a0<a id=\"menurosh\" class=\"fui-Link ___1q1shib f2hkw1w f3rmtva f1ewtqcl fyind8e f1k6fduh f1w7gpdv fk6fouc fjoy568 figsok6 f1s184ao f1mk8lai fnbmjn9 f1o700av f13mvf36 f1cmlufx f9n3di6 f1ids18y f1tx3yz7 f1deo86v f1eh06m1 f1iescvh fhgqx19 f1olyrje f1p93eir f1nev41a f1h8hb77 f1lqvz6u f10aw75t fsle3fq f17ae5zn\" title=\"https:\/\/www.linkedin.com\/company\/azure-cosmos-db\/\" href=\"https:\/\/www.linkedin.com\/company\/azure-cosmos-db\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Link LinkedIn\">LinkedIn<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Global secondary indexes for Azure Cosmos DB\u2014now in Public Preview\u2014make it easier to query data efficiently, especially as your datasets grow.\u00a0 Distributed databases like\u202fAzure Cosmos DB scale with dataset growth by partitioning data across multiple physical machines. When your queries include the\u202fpartition key, the client routes them directly to the appropriate partition. Queries that include [&hellip;]<\/p>\n","protected":false},"author":94159,"featured_media":10283,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[14],"tags":[],"class_list":["post-10247","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-sql-api"],"acf":[],"blog_post_summary":"<p>Global secondary indexes for Azure Cosmos DB\u2014now in Public Preview\u2014make it easier to query data efficiently, especially as your datasets grow.\u00a0 Distributed databases like\u202fAzure Cosmos DB scale with dataset growth by partitioning data across multiple physical machines. When your queries include the\u202fpartition key, the client routes them directly to the appropriate partition. Queries that include [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/10247","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\/94159"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/comments?post=10247"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/10247\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media\/10283"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media?parent=10247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/categories?post=10247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/tags?post=10247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}