Sometimes, we may want to do a quick lookup on attributes, which are not part of the DynamoDB table keys; in that case, we can create a Global Secondary Index, specifying the required attributes as the index keys.
Consider the e-commerce application we have been following up in this book, where we have a product table whose hash and range keys are the id
and type
of the product. Now, consider a use case where you want to have an updated view of the product price and stock. In that case, you can create a GSI on the attribute called the product name, and you will project only the stock and price of the product in the GSI.
We can create the table with a GSI, as we saw in the earlier recipes, with a slight modification. Earlier, we had projected all the attributes; now we can project only the required attributes, such as the stock and price. A sample projection...