英文:
How to set multiple fields as sort keys?
问题
我有一个 DynamoDB 表,我希望将 3 个字段的组合设为唯一值(user-id
、subscription-name
和 year-month
)。如果我将 user-id
和 year-month
设为分区键和排序键,它将不允许我为同一用户的同一月份存储 Netflix 和 Hulu 的订阅名称。
鉴于所有选项及其权衡,哪种方法是最佳的?我有哪些选择?我建议将订阅名称与年份使用分隔符结合起来(例如:netflix:2023-06
)。
英文:
I have a DynamoDB table where I would like a combination of 3 fields to be unique (user-id
, subscription-name
and year-month
). If I make user-id
and year-month
as partition and sort key it will not let me store subscription-name
of Netflix and Hulu for the same month for same user.
Given all options and their trade offs, which one is the best? What are my options? I suggest combining subscription name with year using a delimiter (ex: netflix:2023-06
).
答案1
得分: 1
只需将2个字段组合在一起作为您的排序键:
pk | sk | data |
---|---|---|
userId | 2023-06:netflix | data |
userId | 2023-06:hulu | data |
请注意,排序键首先包含日期,这使您能够执行查询,例如,对于用户1,请给我列出他们在2023年6月的所有订阅,同时仍然满足您的主要访问模式并处理唯一性。
SELECT * FROM MYTABLE WHERE pk=user1 AND sk begins_with(2023-06)
英文:
Simply combine 2 fields for your sort key:
pk | sk | data |
---|---|---|
userId | 2023-06:netflix | data |
userId | 2023-06:hulu | data |
Notice that the sort key contains date first, this allows you to make queries like, for user1 give me all of their subscriptions in June 2023 for example, while still fulfilling your primary access patterns and handling uniqueness.
SELECT * FROM MYTABLE WHERE pk=user1 AND sk begins_with(2023-06)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论