Cosmos DB 平面 VS 嵌套设计
Cosmos DB flat VS nested design
这是 Cosmos DB (SQL API) 查询:
SELECT * FROM c WHERE c.Name = 'John'
比
更快或更便宜
SELECT * FROM c WHERE c.Personal.Name = 'John'
我正在尝试了解设计我的数据平面与嵌套(非规范化与非规范化)的后果。
谢谢
您提到的两个查询版本在成本方面可能非常接近,但根据我的经验,模型复杂性的更重要影响是写入成本。 Cosmos 默认为每个 possible path from the item root. That means the more complex your model, the more paths will be indexed, which directly increases the cost of a write operation. As the indexing docs 个注释创建一个索引:
By optimizing the number of paths that are indexed, you can
substantially reduce the latency and RU charge of write operations.
因此,如果您在具有多个属性的根项目中嵌入一个 Personal
项目,您的项目的编写成本会更高。
人们在 Whosebug 上也提出了很多问题,询问如何为复杂的对象模型编写查询,他们从不喜欢评论“为什么不用更简单的模型?”如果有机会,请避免这种命运。 :)
一般来说,保持项目尽可能简单和小似乎是一个经验法则。与往常一样,测试并查看。查询的 RU 成本是确定性的,因此您可以通过调整变量和 运行 快速测试直接了解更改的影响。
这是 Cosmos DB (SQL API) 查询:
SELECT * FROM c WHERE c.Name = 'John'
比
更快或更便宜SELECT * FROM c WHERE c.Personal.Name = 'John'
我正在尝试了解设计我的数据平面与嵌套(非规范化与非规范化)的后果。
谢谢
您提到的两个查询版本在成本方面可能非常接近,但根据我的经验,模型复杂性的更重要影响是写入成本。 Cosmos 默认为每个 possible path from the item root. That means the more complex your model, the more paths will be indexed, which directly increases the cost of a write operation. As the indexing docs 个注释创建一个索引:
By optimizing the number of paths that are indexed, you can substantially reduce the latency and RU charge of write operations.
因此,如果您在具有多个属性的根项目中嵌入一个 Personal
项目,您的项目的编写成本会更高。
人们在 Whosebug 上也提出了很多问题,询问如何为复杂的对象模型编写查询,他们从不喜欢评论“为什么不用更简单的模型?”如果有机会,请避免这种命运。 :)
一般来说,保持项目尽可能简单和小似乎是一个经验法则。与往常一样,测试并查看。查询的 RU 成本是确定性的,因此您可以通过调整变量和 运行 快速测试直接了解更改的影响。