Choosing the right Partition Key
Choosing a good partition key is crucial for query performance and cost and leads to even data distribution and avoids overloaded partitions. Analyse the data and the query patterns for the right partition key that will align with the queries. The right partition key then will help to perform efficient lookups across partitions. So, pick a partition key value that appears frequently as a filter.
Examples of good partition keys include
Location (city, state etc)
Customer Id or User Id
Product Id
Team Name or Team Id
Date, etc…
Example
To explain the partition key with an example, let us say we have a database of hotels in many different cities. Here the City Name is the key data by which the hotels are organized and searched. So the Partition Key Value is: CityName and Partition Key Path is: /CityName
Below is the steps for creating the Partition Key in Azure Cosmos DB.
Step 1: Goto Azure Cosmos DB NoSQL API and select the new Container option.
Step 2: The new Container option will display a new popup window to create the container with Partition Key.
Select the Database name (Create a new database if not available)
Step 3: Input Container id, example ‘Hotels’. Input the Partition Key, example ‘CityName’.
Press ‘OK’ to create the new container with the partition Key.
Step 4: Add some meaningful and relevant data with partition key values.
Below is how the partition key with data looks.
Step 5: Run a query to fetch data using the partition key. Below is the result:
What is Partition Key In Azure Cosmos DB
The Partition Key in Azure Cosmos DB is an important factor for distributing data efficiently across the database’s physical partitions. The partition key plays an important role in storing and accessing data and the partition key makes the filtering data more efficient and optimized. In this article we will discuss the partition key in Azure Cosmos DB, how it is created, and how it is used in data storage and data retrieval.
Contact Us