https://satonaoki.wordpress.com/2020/09/13/jcdug-cosmos-db-data-modeling/
Data modelling and partitioning in AzureCosmos DB(Azure Cosmos DB でのデータ モデリングとパーティション分割)
View Slide
Session's objectives
What is Azure Cosmos DB?Non-relational and horizontally scalable
What is Azure Cosmos DB?horizontally scalable
What is Azure Cosmos DB?non-relational
What is Azure Cosmos DB?non-relationalandhorizontally scalable
So is Azure Cosmos DB suitable for relationalworkloads?
Let's look at a concrete example
Identifying the operations we have to serve
Now let's implement this model on Azure Cosmos DB!
Starting with the Customer entity
To embed or to reference?
To embed or to reference?------
Our first entity: Customer
CustomercustomersPK: ?
What is partitioning?
What is partitioning?logical partitions
What is partitioning?AndrewTheoMarkTimDeborah Luis
What is partitioning?Max size: 20 GBMax size: 2 MB
What is partitioning?Andrew TheoMarkTimDeborah LuisSELECT * FROM c WHERE c.username = 'Mark'our partition key
What is partitioning?Andrew TheoMarkTimDeborah LuisSELECT * FROM c WHERE c.favoriteColor = 'orange'?
Choosing a partition key for customerscustomersPK: ?
Choosing a partition key for customerscustomersPK: id
Next: product categories
Product categories
Product categoriesproductCategoriesPK: ?
Product categoriesproductCategoriesPK: ?SELECT * FROM c
Product categoriesproductCategoriesPK: type
Next: product tags
Product tags
Product tagsproductTagsPK: ?
Product tagsproductTagsPK: type
Next: products
Products
ProductsproductsPK: ?
ProductsproductsPK: ?CategoryA CategoryCCategoryBSELECT * FROM c WHERE c.categoryId = 'CategoryA'
ProductsproductsPK: categoryIdcategory name?tag names?
Products: how to return category and tag names?productsSELECT * FROM c WHERE c.categoryId = 'CategoryA'productCategoriesSELECT c.name FROM c WHERE c.id = 'CategoryA'productTagsSELECT * FROM cWHERE c.id IN ('', '', '')
Introducing denormalization
Products: denormalizing category and tag namesproductsPK: categoryId
Products: keeping everything in syncproductCategoriesproductTagsproducts
Cosmos DB's change feed
Next: sales orders
Sales orders
Sales orderssalesOrdersPK: ?
Sales orderssalesOrdersPK: ?CustomerA CustomerCCustomerBSELECT * FROM c WHERE c.customerId = 'CustomerA'
Sales orderssalesOrdersPK: customerId
Sales orderssalesOrdersPK: customerIdcustomersPK: id
Mixing entities in the same container?
Sales orders: mixing with customerscustomersPK: id
Sales orders: mixing with customerscustomersPK: customerId
Sales orders: mixing with customersCustomerACustomerCCustomerBcustomer sales orderscustomersPK: customerId
Sales orderscustomersPK: customerIdSELECT * FROM c WHERE c.customerId = 'CustomerA'AND c.type = 'salesOrder'
Sales orderscustomersPK: customerId
Denormalizing the count of sales orders per customer
Denormalizing the count of sales orders per customerCustomerACustomerCCustomerBcustomer sales orderscustomersPK: customerId
Denormalizing the count of sales orders per customerCustomerACustomerCCustomerBupdate the customer add a sales ordercustomersPK: customerId
Denormalizing the count of sales orders per customerCustomerACustomerCCustomerBupdate the customer add a sales order
Sales orderscustomersPK: customerIdSELECT * FROM c WHERE c.type = 'customer'ORDER BY c.salesOrderCount DESC
Our final designcustomersPK: customerIdproductCategoriesPK: typeproductTagsPK: typeproductsPK: categoryId
Our final design, optimized!customersPK: customerIdproductMetaPK: typeproductsPK: categoryId
Key takeaways
Going furtherhttps://docs.microsoft.com/azure/cosmos-db/modeling-datahttps://docs.microsoft.com/azure/cosmos-db/how-to-model-partition-examplehttps://devblogs.microsoft.com/cosmosdb/data-modeling-and-partitioning-for-relational-workloads/https://github.com/AzureCosmosDB/labs/blob/master/readme.mdhttps://github.com/AzureCosmosDB/labs/blob/master/decks/Data-Modeling.pptx