[第2回 Azure Cosmos DB 勉強会] Data modelling and partitioning in Azure Cosmos DB (Azure Cosmos DB でのデータモデリングとパーティション分割)

[第2回 Azure Cosmos DB 勉強会] Data modelling and partitioning in Azure Cosmos DB (Azure Cosmos DB でのデータモデリングとパーティション分割)

0f993881fa8709dbe986bcade6c1fbad?s=128

SATO Naoki (Neo)

September 13, 2020
Tweet

Transcript

  1. Data modelling and partitioning in Azure Cosmos DB (Azure Cosmos

    DB でのデータ モデリングとパーティション分割)
  2. Session's objectives

  3. What is Azure Cosmos DB? Non-relational and horizontally scalable

  4. What is Azure Cosmos DB? horizontally scalable

  5. What is Azure Cosmos DB? non-relational

  6. What is Azure Cosmos DB? non-relational and horizontally scalable

  7. So is Azure Cosmos DB suitable for relational workloads?

  8. Let's look at a concrete example

  9. Identifying the operations we have to serve

  10. Now let's implement this model on Azure Cosmos DB!

  11. Starting with the Customer entity

  12. Starting with the Customer entity

  13. To embed or to reference?

  14. To embed or to reference? - - - - -

    -
  15. Our first entity: Customer

  16. Customer customers PK: ?

  17. What is partitioning?

  18. What is partitioning? logical partitions

  19. What is partitioning? Andrew Theo Mark Tim Deborah Luis

  20. What is partitioning? Max size: 20 GB Max size: 2

    MB
  21. What is partitioning?

  22. What is partitioning?

  23. What is partitioning?

  24. What is partitioning? Andrew Theo Mark Tim Deborah Luis SELECT

    * FROM c WHERE c.username = 'Mark' our partition key
  25. What is partitioning? Andrew Theo Mark Tim Deborah Luis SELECT

    * FROM c WHERE c.favoriteColor = 'orange' ?
  26. Choosing a partition key for customers customers PK: ?

  27. Choosing a partition key for customers customers PK: ?

  28. Choosing a partition key for customers customers PK: id

  29. Choosing a partition key for customers customers PK: id

  30. Next: product categories

  31. Product categories

  32. Product categories productCategories PK: ?

  33. Product categories productCategories PK: ? SELECT * FROM c

  34. Product categories productCategories PK: type

  35. Next: product tags

  36. Product tags

  37. Product tags productTags PK: ?

  38. Product tags productTags PK: ?

  39. Product tags productTags PK: type

  40. Next: products

  41. Products

  42. Products

  43. Products products PK: ?

  44. Products products PK: ? CategoryA CategoryC CategoryB SELECT * FROM

    c WHERE c.categoryId = 'CategoryA'
  45. Products products PK: categoryId category name? tag names?

  46. Products: how to return category and tag names? products SELECT

    * FROM c WHERE c.categoryId = 'CategoryA' productCategories SELECT c.name FROM c WHERE c.id = 'CategoryA' productTags SELECT * FROM c WHERE c.id IN ('<tagId1>', '<tagId2>', '<tagId3>')
  47. Introducing denormalization

  48. Products: denormalizing category and tag names products PK: categoryId

  49. Products: keeping everything in sync productCategories productTags products

  50. Cosmos DB's change feed

  51. Products: keeping everything in sync productCategories productTags products

  52. Next: sales orders

  53. Sales orders

  54. Sales orders

  55. Sales orders salesOrders PK: ?

  56. Sales orders salesOrders PK: ?

  57. Sales orders salesOrders PK: ? CustomerA CustomerC CustomerB SELECT *

    FROM c WHERE c.customerId = 'CustomerA'
  58. Sales orders salesOrders PK: customerId

  59. Sales orders salesOrders PK: customerId customers PK: id

  60. Mixing entities in the same container?

  61. Sales orders salesOrders PK: customerId customers PK: id

  62. Sales orders: mixing with customers customers PK: id

  63. Sales orders: mixing with customers customers PK: customerId

  64. Sales orders: mixing with customers customers PK: customerId

  65. Sales orders: mixing with customers CustomerA CustomerC CustomerB customer sales

    orders customers PK: customerId
  66. Sales orders customers PK: customerId SELECT * FROM c WHERE

    c.customerId = 'CustomerA' AND c.type = 'salesOrder'
  67. Sales orders customers PK: customerId

  68. Denormalizing the count of sales orders per customer

  69. Denormalizing the count of sales orders per customer

  70. Denormalizing the count of sales orders per customer CustomerA CustomerC

    CustomerB customer sales orders customers PK: customerId
  71. Denormalizing the count of sales orders per customer CustomerA CustomerC

    CustomerB update the customer add a sales order customers PK: customerId
  72. Denormalizing the count of sales orders per customer CustomerA CustomerC

    CustomerB update the customer add a sales order
  73. Sales orders customers PK: customerId SELECT * FROM c WHERE

    c.type = 'customer' ORDER BY c.salesOrderCount DESC
  74. Our final design customers PK: customerId productCategories PK: type productTags

    PK: type products PK: categoryId
  75. Our final design, optimized! customers PK: customerId productMeta PK: type

    products PK: categoryId
  76. Key takeaways

  77. Going further https://docs.microsoft.com/azure/cosmos-db/modeling-data https://docs.microsoft.com/azure/cosmos-db/how-to-model-partition-example https://devblogs.microsoft.com/cosmosdb/data-modeling-and-partitioning-for-relational-workloads/ https://github.com/AzureCosmosDB/labs/blob/master/readme.md https://github.com/AzureCosmosDB/labs/blob/master/decks/Data-Modeling.pptx