简体   繁体   English

根据日期与父对象一起查询一对多关系的子对象

[英]Query children of One-To-Many Relationship based on date along with parent

I have two entities in my dynamo table: User and Order.我的发电机表中有两个实体:用户和订单。

Each user has 0..* orders and each order has exactly one associated user .每个user0..*个订单,每个order都有一个关联的user Every order also has a orderDate attribute, that describes when the order was placed.每个order还有一个orderDate属性,用于描述下订单的时间。 My current table is structured as follows to make retrieving all orders for a specific user efficient:我当前的表格结构如下,可以有效地检索特定用户的所有订单:

+--------------+----------------+--------------------------------------+
| PK           | SK             |              Attributes              |
+--------------+----------------+-------------+-----------+------------+
|              |                | name        | firstName | birthDate  |
+--------------+----------------+-------------+-----------+------------+
| USER#userid1 | META#userid1   | Foo         | Bar       | 2000-10-10 |
+--------------+----------------+-------------+-----------+------------+
|              |                | orderDate   |           |            |
+--------------+----------------+-------------+-----------+------------+
| USER#userid1 | ORDER#orderid1 | 2020-05-10  |           |            |
+--------------+----------------+-------------+-----------+------------+

I now have a second access pattern where I want to query all orders (regardless of user) that were placed on a specific day (eg 2020-05-10 ) along with the the user(s) that placed them .我现在有第二种访问模式,我想查询在特定日期(例如2020-05-10 )放置的所有订单(无论用户如何)以及放置它们的用户

I'm struggling to handle this access pattern in my table design.我正在努力在我的表格设计中处理这种访问模式。 Neither GSIs nor different primary keys seem to work here, because I either have to duplicate every user item for each day or I can't query the orders together with the user. GSI 和不同的主键似乎在这里都不起作用,因为我要么必须每天复制每个用户项目,要么不能与用户一起查询订单。

Is there an elegant solution to my problem?我的问题有一个优雅的解决方案吗?

Unfortunately, I can't seem to figure out a way to elegantly solve your problem.不幸的是,我似乎无法找到优雅地解决您的问题的方法。

You need to either duplicate the user info and store in the order record or use a second getItem to query the user-specific info.您需要复制用户信息并存储在订单记录中,或者使用第二个 getItem 来查询用户特定信息。

If anyone has better solutions, please let me know.如果有人有更好的解决方案,请告诉我。

This is a perfect use case for a secondary index.这是二级索引的完美用例。 Here's one way to do it:这是一种方法:

You could create a secondary index (GSI1) on the Order item with a Partition Key (GSI1PK) of ORDERS#<orderDate> and a Sort Key (GSI1SK) of USER#<user_id> .您可以使用ORDERS#<orderDate>的分区键 (GSI1PK) 和USER#<user_id>的排序键 (GSI1SK) 在 Order 项目上创建二级索引 (GSI1)。 It would look something like this:它看起来像这样:

用户订单

The logical view of your GSI1 would look like this: GSI1 的逻辑视图如下所示:

在此处输入图像描述

GSI1 would now support a query of all orders placed on a specific day. GSI1 现在支持查询在特定日期下的所有订单。

Keep in mind that denormalizing your data model (eg repeating user info in the Order item) is a common pattern utilized in DynamoDB data modeling.请记住,非规范化数据 model(例如,在 Order 项目中重复用户信息)是 DynamoDB 数据建模中使用的常见模式。 Remember, space is cheap, More importantly.请记住,空间很便宜,更重要的是。 you are pre-joining your data to support your applications access patterns, In this instance.在这种情况下,您正在预先加入您的数据以支持您的应用程序访问模式。 I'd add whatever User metadata you need to the Order item so it gets projected into the index.我会将您需要的任何用户元数据添加到 Order 项目中,以便将其投影到索引中。

Make sense?说得通?

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

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