bingles
bingles

Reputation: 12203

DynamoDB Adjacency List Pattern

I'm trying to better understand using the adjacency list pattern for many to many (m:n) relationship design in AWS DynamoDB.

Looking at the AWS docs here: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-adjacency-graphs.html we have an example with an Invoice and Bill entity with an m:n relationship.

enter image description here

I understand that I can get details of all bills associated with a particular invoice by reading a single partition. For example I can query for Invoice-92551 and know some attributes of the 2 bills that are associated with it based on the additional items in the partition.

My question is what do I have to do to get the full bill attributes for these 2 bills. Does this require 2 additional queries using the IDs I derived from the invoice partition, or is there some other pattern I am missing here?

Additional Details Referencing the 2 different descriptions of Bill items in the screenshot:

Does this mean that my Invoice partitions should include any Bill attributes I want to access via minimal queries? I was originally thinking the Bill partitions would contain most of what I want, but that doesn't quite make sense if I want to get at them by Invoice.

Upvotes: 5

Views: 1790

Answers (2)

David Harkness
David Harkness

Reputation: 36532

You have two options: issue multiple queries or duplicate some bill data. When you query for an invoice and its bills, you'll get

  • More attributes of this invoice, and
  • Attributes of this bill in this invoice.

You will not get "More attributes of this bill" for any bills. To get those, you must query for the bills themselves. You can issue individual GetItem queries or a single BatchGetItem query with the bill IDs (limited to 100 bills per query).

Alternatively, you can duplicate some values from "More attributes of this bill" to each invoice-bill item to avoid the second query at the cost of storage and insert/update complexity.

Upvotes: 2

Nadav Har'El
Nadav Har'El

Reputation: 13731

No, no additional queries - unless you ask ("project") only certain attributes, your query will retrieve all the attributes of the bills together with its key.

DynamoDB stores each partition together on a single node, so it's efficient to fetch the entire partition. This partition is defined by its "partition key" (your invoice number). The partition contains a bunch of "items" (your bills), each item has its own "sort key" (your bill ID) and any number of "attributes". When DynamoDB reads the partition, it reads those items in order, with all their attributes, and can return all of them unless you specifically asked it not to. Note that even if you ask it only to return a subset (a "projection") of these attributes, Amazon still needs to read them from disk, and you will still pay for this I/O.

Upvotes: 0

Related Questions