I am trying to wrap my head around how I should design a table in DynamoDB. I need a Subscription
table that stores items for each subscription a company has. So I was planning on the following attributes.
I have read quite a lot about how indexing works in DynamoDB in regards to secondary indexes, but I really struggle to make sense of it all. It seems as if the more I read, the more confused I get. I understand the core concepts of global secondary indexes and local secondary indexes as well as hash key + range key. The problem is putting it all together and designing a table that supports my query requirements.
As of now, I have the following query requirements:
CompanyID
and comparing StartDate
and EndDate
to a given dateProductID
In the future (or now), I would like to be able to query for all active subscriptions for a given ProductID
. My initial plan was to use SubscriptionID
as the hash key and the CompanyID
as a range key for efficient queries for a given company's subscriptions. However, I am not sure if it is even possible to query for the range key alone? I know that the combination of the hash key and range key is unique (composite key), but do I always need to include the hash key when performing lookups for the range key?
I was also thinking to make local secondary indexes for StartDate
and EndDate
to efficiently query for a given company's active subscriptions, but I am not sure if I should make these global secondary indexes instead for when I want to query for all of the active subscriptions (for all companies)? My guess would be yes, because I need to query across all partitions and not just the one for a single company.
Like I mentioned, I have a fundamental understanding of the concepts, but my struggle is when it comes to querying tables/indexes and which query patterns each combination of primary keys and global/local secondary indexes make possible/impossible. I find it hard to grasp the combination of a key-value store with the added capabilities that secondary indexes provide. I would really love if anyone could give me an example of how they would design the table and explain why, such as how the various choices support my particular query requirements.
I am aware that much of this stuff is documented, but I am struggling to use this information in relation to this scenario. I hope anyone can help. So: how would you design the table to support the queries that I listed and why?
Here's a proposal, given some assumptions:
Table design:
Global secondary indexes:
Queries:
query by company for active subscriptions on a given date: use CompanyID-StartDate-index, query CompanyID = :companyid, StartDate <= :date
, add filter EndDate >= :date
query by company for active subscriptions for a given product on a given date: use CompanyID-ProductID-index, query CompanyID = :companyid, ProductID = :productid
, add filter StartDate <= :date
, add filter EndDate >= :date
query for active subscriptions for a given product on a given date: use ProductID-StartDate-index, query ProductID = :productid, StartDate <= :date
, add filter EndDate >= :date
You can try all of this out fairly quickly in the AWS DynamoDB console:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With