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.
- SubscriptionID
- CompanyID
- StartDate
- EndDate
- ProductID
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:
- Being able to query for active subscriptions for a given company. That is, querying by
CompanyID
and comparingStartDate
andEndDate
to a given date - The same as the above, except querying to see if a given company has an active subscription for a specific
ProductID
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?
SubscriptionID
is globally unique. There won't be so many subscriptions per company; maximum a few hundreds I'd say, but that will be very rare. I could implement this in a relational database, sure, and now that you mention it, it might not be a bad choice. However, my confusion still applies for many similar use cases where the use of a NoSQL database is more appropriate. Therefore, I would greatly appreciate any advice and clearing out my confusion regardless. Thanks! - ba0708