{"id":7714,"date":"2024-03-27T07:00:44","date_gmt":"2024-03-27T14:00:44","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cosmosdb\/?p=7714"},"modified":"2024-04-15T03:09:18","modified_gmt":"2024-04-15T10:09:18","slug":"optimizing-database-performance-change-partition-keys-of-a-container-in-azure-cosmos-db-for-nosql-api-preview","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cosmosdb\/optimizing-database-performance-change-partition-keys-of-a-container-in-azure-cosmos-db-for-nosql-api-preview\/","title":{"rendered":"Optimizing database performance: Change partition keys of a container in Azure Cosmos DB for NoSQL API (preview)"},"content":{"rendered":"<p><a href=\"https:\/\/learn.microsoft.com\/azure\/cosmos-db\/partitioning-overview\" target=\"_blank\" rel=\"noopener\">Partitioning<\/a> is a key concept in Azure Cosmos DB, a fully managed, globally distributed, multi-model database service that offers unlimited scalability and low latency. By distributing data across multiple physical partitions based on the partition key(s), Azure Cosmos DB optimizes data access and distribution, profoundly impacting database performance and cost. Therefore, choosing the right partition key is paramount to achieving the right performance and cost.<\/p>\n<h4>Challenges with partition keys<\/h4>\n<p>Choosing the right partition key is not always easy. Sometimes, the partition key that is initially chosen may not be optimal for the query patterns or the data distribution as the application evolves over time. This can lead to issues such as:<\/p>\n<ul>\n<li><strong>Hot partitions<\/strong>, which are partitions that receive more requests than others and can cause throttling and\/or high costs.<\/li>\n<li><strong>Cross-partition queries<\/strong>, which are queries that span multiple partitions and can have higher latency and costs than queries that target a single partition.<\/li>\n<\/ul>\n<p>In such cases, changing the partition key may be necessary to optimize the performance and cost of the database.<\/p>\n<p>In this blog post, we will introduce a new feature in the Azure Portal that allows changing the partition key of a container in Azure Cosmos DB easily with a few clicks. This feature uses the container copy API in the background, which copies the data from the source container to the destination container.<\/p>\n<h3>How partition key change rescues<\/h3>\n<p>Changing the partition key of a container in Azure Cosmos DB is not a trivial operation. It requires creating a new container with the desired partition key and copying the data from the existing container to the new one using multiple disparate tools. This process can take time and incur additional costs depending on the size and throughput of the data.<\/p>\n<p>To make this process easier and more convenient, the Azure Portal provides a new feature that allows changing the partition key of a container with a few clicks.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/03\/cosmosdb-change-pk.png\"><img decoding=\"async\" class=\"aligncenter wp-image-7719 size-full\" src=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/03\/cosmosdb-change-pk.png\" alt=\"Image cosmosdb change pk\" width=\"2371\" height=\"1060\" srcset=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/03\/cosmosdb-change-pk.png 2371w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/03\/cosmosdb-change-pk-300x134.png 300w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/03\/cosmosdb-change-pk-1024x458.png 1024w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/03\/cosmosdb-change-pk-768x343.png 768w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/03\/cosmosdb-change-pk-1536x687.png 1536w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/03\/cosmosdb-change-pk-2048x916.png 2048w\" sizes=\"(max-width: 2371px) 100vw, 2371px\" \/><\/a><\/p>\n<p>After clicking on the change button, user needs to either create a new container or select an existing one from the same database. If user creates a new container, then all configurations except partition keys and unique keys are replicated from the source container to the new destination container.<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/03\/cosmosdb-change-pk-create-container.png\"><img decoding=\"async\" class=\"aligncenter wp-image-7716 size-medium\" src=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/03\/cosmosdb-change-pk-create-container-241x300.png\" alt=\"Image cosmosdb change pk create container\" width=\"241\" height=\"300\" srcset=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/03\/cosmosdb-change-pk-create-container-241x300.png 241w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/03\/cosmosdb-change-pk-create-container-823x1024.png 823w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/03\/cosmosdb-change-pk-create-container-768x956.png 768w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/03\/cosmosdb-change-pk-create-container.png 834w\" sizes=\"(max-width: 241px) 100vw, 241px\" \/><\/a><\/p>\n<p>The portal then uses the container copy API in the background, which copies the data from the source container to the destination container easily without any manual intervention.<\/p>\n<h3>Monitoring progress<\/h3>\n<p>The user can monitor the progress and status of the copy operation in the portal and cancel it if needed. Once the copy is completed, the user can switch the application to use the new container with the updated partition key and delete the old one.<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/03\/change-pk-monitoring.png\"><img decoding=\"async\" class=\"aligncenter wp-image-7717 size-large\" src=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/03\/change-pk-monitoring-1024x681.png\" alt=\"Image change pk monitoring\" width=\"640\" height=\"426\" srcset=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/03\/change-pk-monitoring-1024x681.png 1024w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/03\/change-pk-monitoring-300x199.png 300w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/03\/change-pk-monitoring-768x511.png 768w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/03\/change-pk-monitoring.png 1053w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<h3>Important considerations<\/h3>\n<p>While this feature can be very useful, some things to keep in mind before using it are:<\/p>\n<ul>\n<li>No writes or deletes should be performed on the source container while the copy job is in progress to ensure the integrity of the data being copied.<\/li>\n<li><span class=\"TextRun SCXW218975918 BCX8\" lang=\"EN-IN\" xml:lang=\"EN-IN\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW218975918 BCX8\">The copy operation may fail due to <\/span><span class=\"NormalTextRun SCXW218975918 BCX8\">various reasons<\/span><span class=\"NormalTextRun SCXW218975918 BCX8\">, such as network issues, throttling, or validation errors. The user should check the status and error messages in the portal and retry<\/span><span class=\"NormalTextRun SCXW218975918 BCX8\">\u00a0<\/span><span class=\"NormalTextRun SCXW218975918 BCX8\">the <\/span><span class=\"NormalTextRun SCXW218975918 BCX8\">change partition key process if needed<\/span><span class=\"NormalTextRun SCXW218975918 BCX8\">.<\/span><\/span><\/li>\n<li>The user is responsible for switching the application to use the new container once the copy is completed and deleting the old container if not needed.<\/li>\n<\/ul>\n<h3>Next Steps<\/h3>\n<p>With the ability to change partition keys in Azure Cosmos DB, users can adapt and optimize their database structures with ease. This feature empowers users to navigate the evolving landscape of database management, unlocking enhanced performance and cost efficiency.<\/p>\n<p>If you have any questions or comments, please feel free to leave them below or contact us through the support channels. Thank you for reading and happy coding!<\/p>\n<ul>\n<li>Check out the documentation for <a href=\"https:\/\/aka.ms\/cosmosdb-change-pk\">changing partition key.<\/a><\/li>\n<li>Explore <a href=\"https:\/\/learn.microsoft.com\/azure\/cosmos-db\/container-copy\" target=\"_blank\" rel=\"noopener\">how container copy works<\/a>.<\/li>\n<li>Deep dive into <a href=\"https:\/\/learn.microsoft.com\/azure\/cosmos-db\/partitioning-overview#choose-partitionkey\" target=\"_blank\" rel=\"noopener\">choosing the right partition key<\/a>.<\/li>\n<li>Learn more about <a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/cosmos-db\/scaling-provisioned-throughput-best-practices\" target=\"_blank\" rel=\"noopener\">Scaling best practices<\/a>.<\/li>\n<\/ul>\n<h4>About Azure Cosmos DB<\/h4>\n<p>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<a href=\"https:\/\/cosmos.azure.com\/try\/\" target=\"_blank\" rel=\"noopener\">Try Azure Cosmos DB for free here.<\/a>\u00a0To stay in the loop on Azure Cosmos DB updates, follow us on\u00a0<a href=\"https:\/\/twitter.com\/AzureCosmosDB\" target=\"_blank\" rel=\"noopener\">X<\/a>,\u00a0<a href=\"https:\/\/aka.ms\/AzureCosmosDBYouTube\" target=\"_blank\" rel=\"noopener\">YouTube<\/a>, and\u00a0<a href=\"https:\/\/www.linkedin.com\/company\/azure-cosmos-db\/\" target=\"_blank\" rel=\"noopener\">LinkedIn<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Partitioning is a key concept in Azure Cosmos DB, a fully managed, globally distributed, multi-model database service that offers unlimited scalability and low latency. By distributing data across multiple physical partitions based on the partition key(s), Azure Cosmos DB optimizes data access and distribution, profoundly impacting database performance and cost. Therefore, choosing the right partition [&hellip;]<\/p>\n","protected":false},"author":90111,"featured_media":61,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[14,1828,996,19],"tags":[],"class_list":["post-7714","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-sql-api","category-data-modeling","category-migration","category-tips-and-tricks"],"acf":[],"blog_post_summary":"<p>Partitioning is a key concept in Azure Cosmos DB, a fully managed, globally distributed, multi-model database service that offers unlimited scalability and low latency. By distributing data across multiple physical partitions based on the partition key(s), Azure Cosmos DB optimizes data access and distribution, profoundly impacting database performance and cost. Therefore, choosing the right partition [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/7714","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\/90111"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/comments?post=7714"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/7714\/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=7714"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/categories?post=7714"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/tags?post=7714"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}