Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does DynamoDB GSI treat missing attributes?

Looking at this DynamoDB documentation about GSI, I found the following comment:

A global secondary index only keeps track of data items where its key attribute(s) actually exist.

Which of the following does this exactly mean?

  1. Missing Partition Key and/or Sort Key from GSI point of view will result in no additional item in GSI
    e.g.) "GameTitle" and "TopScore" are required
  2. Missing data for any attributes you specify as a part of GSI with INCLUDE option will result in no additional item in GSI
    e.g.) All attributes projected to GSI even "Wins", "Loses" are required

I'm suspecting the "key attribute(s)" refer to the 1., and any missing data from INCLUDE option point of view will simply come back as empty when GSI is queried, but wanted to check if my understanding is correct.

Also, would there be no difference between GSI and LSI in this space?

like image 314
Ryota Avatar asked Jan 05 '19 03:01

Ryota


People also ask

How does GSI work in DynamoDB?

For global secondary index queries, DynamoDB calculates the provisioned read activity in the same way as it does for queries against tables. The only difference is that the calculation is based on the sizes of the index entries, rather than the size of the item in the base table.

Can GSI be null in DynamoDB?

If the value of a global secondary index key attribute is null or empty, it is better to just skip the attribute when writing it. Because global secondary indexes are stored separately, if you skip writing null or empty attributes they are not projected to the global secondary index, saving storage and write cost.

Does GSI need to be unique?

A GSI does not need to have a sort key element; we may have only a partition key (may not be unique). Unlike the primary key on a table, a GSI index does not require the indexed attributes to be unique.

Can DynamoDB attributes be null?

You cannot use UpdateItem to update any primary key attributes. Instead, you will need to delete the item, and then use PutItem to create a new item with new attributes. Attribute values cannot be null; string and binary type attributes must have lengths greater than zero; and set type attributes must not be empty.


1 Answers

In the page you linked to Global Secondary Indexes

Then next two lines from what you quoted are:

A global secondary index only keeps track of data items where its key attribute(s) actually exist. For example, suppose that you added another new item to the GameScores table, but only provided the required primary key attributes:
Because you didn't specify the TopScore attribute, DynamoDB would not propagate this item to GameTitleIndex.

So if you have a GSI over attribute GSIKey and you add a record to the table without that attribute, the GSI will not get an entry for that record.

If you add a record with a GSIKey, then the GSI will have an entry for that record.

Any additional projected attributes will either be there or not. Same as with the table itself.

The technical term for this is a sparse index; it does not have to contain as many entries as the base table.

Local secondary index are also sparse.

like image 191
Charles Avatar answered Sep 25 '22 17:09

Charles