I am trying to create a table to store invoice line items in DynamoDB. Let's say the item is defined by CompanyCode
, InvoiceNumber
and LineItemId
, amount and other line item details.
A unique item is defined by the combination of the first 3 attributes. Any 2 of those attributes can be same for the different items. What should I select as the Hash Attribute and the Range Attribute?
DynamoDB uses primary keys to uniquely identify each item in a table and secondary indexes to provide more querying flexibility.
DynamoDB uses three basic data model units, Tables, Items, and Attributes. Tables are collections of Items, and Items are collections of Attributes. Attributes are basic units of information, like key-value pairs.
Attributes in DynamoDB are similar in many ways to fields or columns in other database systems. Partition key and sort key: Referred to as a composite primary key, this type of key is composed of two attributes. The first attribute is the partition key, and the second attribute is the sort key.
Primary Key can be on one column or composite. Composite primary key consists of Partition Key and Sort Key. Partition key is used as input to the hashing function that will determine partition of the items.
For efficiency I would propose totally different design. With NoSQL databases (and DynamoDB is not different) we always need to consider the access patterns first. Also, if possible we should strive to fit all our data within same table and several indexes. From what we have from OP and his comments, these are the two access patterns:
We now wonder what is a good Primary Key? Translates to question what is a good Partition Key (PK) and what is a good Sort Key (SK) and which secondary indexes do we need to create and of what kind (local or global)? Some reminders:
KeyConditionExpression
which provides you with set of operators for sorting and everything in between (one of them being function begins_with (a, substr)
)FilterExpression
if you need to further refine the Query results (filter on the projected attributes)It is obvious that we are dealing with multiple entities that need to be modeled and fit into the same table. To satisfy condition of Partition Key being unique on the table, CompanyCode
comes as a natural Partition Key - so I would ensure that is unique. If not then you need to ask yourself how can you model the second access pattern?
Assuming we have established uniqueness on the CompanyCode
let's simplify and say that it comes in the form of an e-mail (or could be domain or just a code, but I will use email for demonstration).
I propose design as in the image below:
CompanyCode
and SK being InvoiceNumber
can store all attributes about that invoice for that company.Customer
which allows me to store all attributes about the company. InvoiceNumber
) and my GSI1SK is my tables PK (CompanyCode
).LineItemId
and SK being CompanyCode
(still unique)InvoiceNumber
and my GSI1SK is LineItemId
which is tables PK so its same as for Invoice entity items.Now the access patterns supported with this:
CompanyCode=X
and use KeyConditionExpression
with =
operator on the Sort Key InvoiceNumber
. If I want to get all the items tied to that invoice, I will project Items
attribute using ProjectionExpression
. BatchGetItem
API call (using my unique composite key LineItemId+CompanyCode
) on table to get all items belonging to that particular invoice of that particular customer. (this comes with some constraints of BatchGetItem API)CompanyCode=X
on PK and use KeyConditionExpression
on the SK with begins_with (a, substr)
function/operator to get only invoices for company X and not the metadata about that company. That will give me all invoices for given company/customer.InvoiceNumber
I can easily select all the line items that belong to that particular invoice. REMEMBER: The key values in a global secondary index do not need to be unique - so in my GSI1 I could have had easily invoice_1 -> (item_1, item_2) and then another invoice_1 -> (item_1,item_2) but the difference between two items in GSI would be in the SK (it would be associated with different CompanyCode
(but for demonstration purposes I used invoice_1 and invoice_2). I believe the first option offered by @georgeaf99 won't work, because if you do it that way, then CompanyCode
has to be unique in the table. Therefore, there would only be one item allowed per company. I think the second solution is the only real way to do it.
You can use CompanyCode
as the Hash Key, and then all other fields that combine to make the item unique (in this case InvoiceNumber
and LineItemId
) need to be somehow combined into one value (such as concatenation with a field delimiter), which would be your Range Key. Unfortunately that is kind of ugly, but that's the nature of a NoSQL database like DynamoDB. However, it will allow you to successfully store records with the correct uniqueness. When reading the records back, if you don't want to parse the combined field back out to its individual parts, then you'll have to add additional separate fields for InvoiceNumber
and LineItemID
.
If you don't have a large number of invoices per company, you can query by only the Hash Key and do the filtering on the client side. If you have a large number of invoices per company and need to be able to query only the items for a single invoice, then I would create a secondary index on CompanyCode and InvoiceNumber.
As I'm sure you have figured out you cannot have more than two attributes form your primary key (hash+range). Thus, depending on the type of queries you will be performing and the size of your data you can structure your table in different ways.
(Optimized for the query type you mentioned above: only CompanyCode
& all 3)
Best sol'n for small/medium size data sets:
CompanyCode
CompanyCode
and
then filter your results on the other two attributesOptimal solution for large data sets:
CompanyCode
InvoiceNumber
+LineItemId
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