Partition Key in cosmos DB

Agnieszka Maria Mietz-Blijleven 25 Reputation points
2023-08-04T21:44:52.83+00:00

Hi,

If the most common operation will display a product catalog that has products grouped by product category.

Which property should I use for the partition key?

Is that productID or productCategoryID?

Thank you in advanced

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,838 questions
0 comments No comments
{count} vote

2 answers

Sort by: Most helpful
  1. Sedat SALMAN 14,160 Reputation points MVP
    2023-08-04T21:48:42.52+00:00

    According to the information you have provided, productCategoryID would be a better partition key choice. Partition keys are used by Azure Cosmos DB to distribute data and workload evenly across multiple physical partitions. The partition key you select determines data distribution and transaction scope. If you use productID as the partition key, each product will have its own partition. While this may be ideal for operations that only target a single product, it does not appear to correspond to your most common operation. If you use productCategoryID as the partition key, all products in the same category will be in the same partition. This will be more efficient for querying and transactions for operations involving groups of products from the same category.

    1 person found this answer helpful.
    0 comments No comments

  2. Oury Ba-MSFT 20,716 Reputation points Microsoft Employee
    2023-08-08T18:03:25.33+00:00

    @Agnieszka Maria Mietz-Blijleven Thank you for reaching out.

    in addition to @Sedat SALMAN

    When choosing a partition key, it's important to consider the access patterns of your application. You should choose a partition key that is frequently used in queries and has a high cardinality. You should also choose a partition key that is immutable and has a small size.

    If the most common operation is to display a product catalog that has products grouped by product category, then you should consider using the productCategoryID as the partition key.

    Please read more here Partition key examples for a better understanding on partition key in Aure cosmos DB

    Regards,

    Oury

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.