简体   繁体   English

DynamoDB 单表设计:如何获取一个类型的所有实体

[英]DynamoDB Single Table Design: How to get all entities of a type

I am on the way creating a Single Table DynamoDB Design for relevant entities in my application.我正在为我的应用程序中的相关实体创建单表 DynamoDB 设计。 I mainly borrowed ideas from here: https://github.com/aws-samples/amazon-dynamodb-design-patterns/blob/master/data-models/an-online-shop/AnOnlineShop.md我主要借鉴了这里的思路: https://github.com/aws-samples/amazon-dynamodb-design-patterns/blob/master/data-models/an-online-shop/AnOnlineShop.md

While most of the things are clear to me, one simple thing is missing for me.虽然大多数事情对我来说都很清楚,但我缺少一件简单的事情。 How can i query all entities of a certain type?我如何查询某种类型的所有实体? See the image below for the schema.有关架构,请参见下图。

在此处输入图像描述

As you can see, the main entity records have PK and SK with the same values.如您所见,主要实体记录的 PK 和 SK 具有相同的值。 SK only changes when you want to have 1:N relations to other entities, as seen in the entityType warehouseItem. SK 仅在您希望与其他实体具有 1:N 关系时发生变化,如 entityType warehouseItem 中所示。

My main question is: How do i get all users (fast without scan of course)?我的主要问题是:如何获得所有用户(当然无需扫描即可快速获取)?

What i learned is that i can't do a begins_with on a PartitionKey.我了解到我不能在 PartitionKey 上执行 begins_with。 Of course i could think of doing records like this:我当然可以想到做这样的记录:

PK       SK
users   u#logemann
users   u#smith

But this would require additional data entries and would be kind of duplicates to my main user records seen above.但这将需要额外的数据条目,并且会与上面看到的我的主要用户记录重复。 Or is it possible to create a GSI with a constant value of "users" for PK and the value of SK for SK?或者是否有可能创建一个 GSI,其中 PK 的“用户”值为恒定值,SK 的值为 SK?

Whats the best way to solve this?解决这个问题的最佳方法是什么?

I think the easiest way is to add "EntityType" in a GSI as PartitionKey and "PK" as SortKey.我认为最简单的方法是在 GSI 中添加“EntityType”作为 PartitionKey 和“PK”作为 SortKey。 This way i have all my entities queryable in a performant way.通过这种方式,我可以以一种高性能的方式查询我的所有实体。

You can have one GSI and then overload it.您可以拥有一个 GSI,然后对其进行超载。 You can get more information from: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-gsi-overloading.html您可以从以下位置获得更多信息: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-gsi-overloading.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM