

Tip 335 - How to choose a partition key in Azure Cosmos DB
source link: https://microsoft.github.io/AzureTipsAndTricks/blog/tip335.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

# How to choose a partition key in Azure Cosmos DB
# Azure Cosmos DB is highly scalable
One of the best features of Azure Cosmos DB (opens new window) is that it's incredibly fast. And that is because it uses a partitioning system (opens new window) to scale, which consists of physical and logical partitions.
To optimize the scalability and performance of Azure Cosmos DB, you need to choose the right partition key (opens new window) for your container. In this post, we'll go through the best practices to choose your partition key.
# Prerequisites
If you want to follow along, you'll need the following:
- An Azure subscription (If you don't have an Azure subscription, create a free account (opens new window) before you begin)
# Choosing your partition key
You choose a partition key when you create a container in Azure Cosmos DB. Note that you can't change the partition key of a container. If you do need to change it, you need to migrate the container data to a new container with the correct key.

A partition key consists of a path, like "/firstname", or "/name/first". You can use alphanumeric and underscore characters in the path.

The partition key needs to be a JSON property in the documents that you store in the container. So in the JSON snippet below, the partition key path could, for instance, be firstname.
{
"firstname": "Véronique",
"age": 50,
"id": "61fb344e-279d-4185-957d-e88bb1101dba",
"_rid": "mYw7AJk8AbkDAAAAAAAAAA==",
"_self": "dbs/mYw7AA==/colls/mYw7AJk8Abk=/docs/mYw7AJk8AbkDAAAAAAAAAA==/",
"_etag": "\"03000091-0000-0700-0000-607d80580000\"",
"_attachments": "attachments/",
"_ts": 1618837592
}
2
3
4
5
6
7
8
9
10
So how do you pick the best partition key for your container?
- You should pick a property that you do not update later
- The partition key should be a property that exists in every document in the container
- The partition key property should have a large range of possible values. This optimizes the amount of logical partitions that the key creates. "id" has a lot of values
- The value of the partition key can be of string or numeric types
Here's the exception to the best practices above: If your container is large and read-heavy (i.e., more then 30.000RUs and larger than 100GB), the key should be something that is often filtered on in queries. For instance, if your queries filter on "postalcode" often that could be a good partition key.

# Conclusion
When you choose the right partition key for your Azure Cosmos DB container (opens new window), you optimize performance. Try it out!
Recommend
-
6
BTC五分钟内上涨1.01%,现报$18,335.94 2020-12-09 08:12:00 BTC五分钟内上涨1.01%,上涨金额为183.42美元,其中火币全球站上现价为$18,335.94,请密切关注行情走向,注意控制风险。
-
12
335 Marketing Specialist jobs in Ukraine (15 new)Skip to main contentTurn on job alerts
-
4
Rocket Accelerated geek conversation #335: AIRBN-BS May 27th, 2021 · 60 minutes Christi...
-
9
【运动护膝裤】在美亚站上的防侵权预警335-跨境头条-AMZ123亚马逊导航-跨境电商出海门户 【运动护膝裤】在美亚站上的防侵权预警335...
-
186
# How to copy data from one Cosmos DB container to another container
-
11
Codeforces Round #335 Problem Analysis Codeforces Round #335 Problem Analysis
-
3
About this Episode Subrat and Chuck sit down and discuss what they're working on and where they're heading going forward. They've both changed work situations and are doing new things. The...
-
7
Koenigsegg's Tiny Electric Motor Makes 335 HP and 443 LB-FT of Torque Koenigsegg Swedish hypercar maker Koenigsegg has long been a home for novel engineer...
-
5
About this Episode Steph has a question for Chris: When you have no idea how you're going to implement a feature, how do you write your first test? Chris has thoughts about hybrid teams (re...
-
9
本文已收录到 AndroidFamily,技术和职场问题,请关注公众号 [彭旭锐] 提问。 大家好,我是小彭。 昨晚是 LeetCode 第 335...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK