In the first picture you posted the items in the base table (primary key) would look like this:
First_id(Partition key) Second_id(Sort Key) Dated
------------- ---------- ------
Invoice-92551 Invoice-92551 2018-02-07
Invoice-92551 Bill-4224663 2017-12-03
Invoice-92551 Bill-4224687 2018-01-09
Invoice-92552 Invoice-92552 2018-03-04
Invoice-92552 Bill-4224687 2018-01-09
Bill-4224663 Bill-4224663 2018-12-03
Bill-4224687 Bill-4224687 2018-01-09
And the same items in the GSI the items would look like this
Second_id(Partition Key) First_id
---------- ---------------
Invoice-92551 Invoice-92551
Bill-4224663 Invoice-92551
Bill-4224687 Invoice-92551
Invoice-92552 Invoice-92552
Bill-4224687 Invoice-92552
Bill-4224663 Bill-4224663
Bill-4224687 Bill-4224687
They have drawn it in quite a confusing way.
- They have merged the partition keys into one box, but they are separate items.
- They have also tried to show the GSI in the same picture. You can think of the base table and the GSI as two separate tables that are kept in sync, in many ways that's what they are.
- They haven't actually provided a name to the key attributes. In my example I have named them First_id and Second_id.
When you do a query on the base table, you can use a query with the partition key Invoice-92551
and you get both the Invoice item plus all the bill items that belong to it.
Imagine you are viewing invoice Invoice-92551
in an application and you can see it has two associated bills (Bill-4224663
and Bill-4224687
). If you clicked on the bill, the application would probably do a query on the GSI. The GSI query would have partition key Bill-4224687
. If you look at the GSI table I have drawn above, you can see this will return two items, showing that Bill-4224687
is part of two invoices (Invoice-92551
and Invoice-92552
)
In your second picture, the words 'bookID' and 'albumID' etc are supposed to represent actual IDs (lets say 293847 and 3340876).
I would draw his example like this:
ProductID(Partition Key) TypeID(Sort Key) Title Name
--------- ------ ------ ------
Album1 Album1 Dark Side
Album1 Album1:Track1 Speak to me
Album1 Album1:Track2 Breathe
Movie8 Movie8 Idiocracy
Movie8 Movie8:Actor1 Luke Wilson
Movie8 Movie8:Actor2 Maya Rudolph
Here are your queries:
Partition key: Album1
Gives you ALL the information (inc tracks) on Album 1 (Dark Side)
Partition key: Album1 and Sort Key: Album1:Track2
Gives you just the information on Breathe.
Partition key: Movie8
Gives you ALL the information (inc actors) on Movie8 (Idiocracy)
If I was building the table I would make it so the words Movie, Album etc were part of the actual ID (say Movie018274 and Album983745987) but that's not required, it just makes the IDs more human readable.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…