如何将多个字段设置为排序键?

huangapple go评论63阅读模式
英文:

How to set multiple fields as sort keys?

问题

我有一个 DynamoDB 表,我希望将 3 个字段的组合设为唯一值(user-idsubscription-nameyear-month)。如果我将 user-idyear-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)

huangapple
  • 本文由 发表于 2023年6月29日 14:48:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76578644.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定