具有复合键的 DynamoDB 架构设计

DynamoDB Schema design with composite keys

我是第一次尝试使用 DynamoDB,在阅读了有关在 DynamoDB 中设计架构的最佳实践后,我遇到了一些似乎无法找到解决方案的问题。

我有一个关系数据库,其结构如下:

#org table
-------------------------------------------------------
orgID       |   orgAttributes
-------------------------------------------------------

#org -> dept team
-------------------------------------------------------
orgID       |   deptName        |   teamAttributes
-------------------------------------------------------

#org -> campaign table
-------------------------------------------------------
orgID       |   campaignID      |   campaignAttributes
-------------------------------------------------------

#campaign -> campaign group
-------------------------------------------------------
campaignID  |   groupID         |   groupAttributes
-------------------------------------------------------

#campaign -> customer list
-------------------------------------------------------
groupID     |   customerID      |   customerAttributes
-------------------------------------------------------

所有这些都是一对多映射到 child table。我阅读了 AWS 的文档并计划实施复合排序键的使用

我的 table 设计应该类似于:

#dynamo attempt
-------------------------------------------------------------------------------------
PK          |   SK                              |   attributes
-------------------------------------------------------------------------------------
ORG#orgID   |   METADATA#orgID                  |   orgAttributes
            |   DEPT#deptName                   |   deptAttributes
            |   CAMPAIGN#campaignID             |   campaignAttributes
            |   CAMPAIGN#CAMPAIGNGROUP#groupID  |   groupAttributes + list<customer>

现在我感到困惑的部分是会有 4 个 table 像:

org table   
{
    PK: ORG#orgID 
    SK: METADATA#orgID
    orgattribute1: 123
    orgattribute2: 123
}

dept table  
{
    PK: ORG#orgID 
    SK: DEPT#deptName
    deptattribute1: 123
    deptattribute2: 123
}

campaign table
{
    PK: ORG#orgID 
    SK: CAMPAIGN#campaignID    
    campaignattribute1: 123
    campaignattribute2: 123
}

campaign group table
{
    PK: ORG#orgID 
    SK: CAMPAIGN#CAMPAIGNGROUP#groupID    
    customerList: { object containing list of customers }
    campaigngroupattribute1: 123
    campaigngroupattribute2: 123
}

或一个 table 仅适用于组织,其他所有内容都作为 object 在 json:

org table
{
    PK: ORG#orgID 
    SK: METADATA#orgID
    orgattribute1: 123
    orgattribute2: 123
    dept: {dept object}
    campaign: {campaign object}
    campaigngroup: {campaign group object}
}

根据应用程序为每个实体处理的数据量,您可以决定是否需要使用单个或多个 DynamoDB table。因此,毫无疑问,根据您当前的要求,继续使用单发电机 table.

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
PK                                                  |   SK                              |   attribute 1               |   attribute2              |     ...    | attributeN
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ORG#orgID                                           |   METADATA#orgID                  |   OrgAttribute 1 Value      |   OrgAttribute 2 Value    |
ORG#orgID                     ̥                      |   DEPT#deptName                   |   deptAttribute 1 Value     |   deptAttribute 2 Value   |
ORG#orgID                                           |   CAMPAIGN#campaignID             |   campaignAttribute 1 Value |   ...
ORG#orgID#CAMPAIGN#campaignID                       |   CAMPAIGNGROUP#groupID           |   groupAttribute 1 Value    |   ...
ORG#orgID#CAMPAIGN#campaignID#CAMPAIGNGROUP#groupID |   CUSTOMER#customerID             |   customerAttribute 1 Value |   ...

这样就可以实现一对多的关系。同样,您可以在未来集成更多实体,如员工、产品等...

例如:如果您希望所有的 CAMPAIGNGROUP 都在一个特定的 campaignID 下。然后,您可以使用 ORG#orgID#CAMPAIGN#campaignID 的 PK 和 begin_with('CAMPAIGNGROUP#')

的 SK 直接查询(有效方式)数据

同样,您可以使用此 table 结构检查其他要求。还要检查 DynamoDB LSI 和 GSI 功能,以便高效地索引数据和查询数据。