1
votes

How is it possible that DynamoDB can create a global secondary index comprising of a partition key (and sort key) that not all items in the table share? I'm not able to put an item into a table that doesn't have the proper primary key structure, so it doesn't make sense to me that I can create a GSI using an attribute as the partition key that not all items in the table have.

Let's say I have a table with 10 items. 5 items are users. 2 items are the products I sell. The 3 remaining items are orders that have been created.

Let's also say the attempt at approaching this NoSQL style was whole-hearted and half-understood, leaving us with these items and their corresponding attributes -

user items: id, email, phone, address, first_name, last_name

product items: id, description, title, price

order items: id, user_id, product_id, quantity, creation_date

The partition key of this table is id.

Then, when I try to create a GSI using creation_date as the partition key, it lets me... can someone explain why this works to me?

1

1 Answers

1
votes

The items that don't have creation_date attribute won't be included in the GSI. What matters is that all items that have creation_date do have id. The idea, in this case, of the GSI is so you can query your items/data by creation_date which otherwise you couldn't since that attribute is not a part of the partition key.

Here is more on GSIs

in particular:

If a table contains an item where a particular attribute is not defined, but that attribute is defined as an index partition key or sort key, DynamoDB doesn't write any data for that item to the index.