Database Reference
In-Depth Information
Secondary indexes
A primary key attribute allows us to create, maintain, and access data efficiently. To do so,
DynamoDB creates indexes on those primary attributes as we have seen in the previous
section. However, sometimes there might be a need to search and access items from attrib-
utes that are not part of the primary key. For all such needs, DynamoDB supports second-
ary indexes that can be created on attributes other than the primary key and can be accessed
and searched in a manner similar to the primary key.
A secondary index contains a subset of attributes from the given table having an alternative
key to support query operations. Secondary index allows users to search attributes other
than the primary key, which makes DynamoDB useful for a varied set of applications. We
can create multiple secondary indexes for a given table. If we don't create secondary in-
dexes, the only option to get the item for a certain non-primary key attribute is to scan the
complete table, which is a very expensive operation.
Internally, when you create a secondary index, you have to specify the attributes to be pro-
jected or included as part of the secondary index. DynamoDB copies those attributes to the
index. Querying a secondary index is similar to querying a table with the hash and range
key. You have to specify the hash key and the optional range for every new index that you
create. These secondary indexes are maintained by DynamoDB itself, so if we can add,
modify, or delete indexed attributes, it gets reflected on the index as well. One thing to note
here is that you need to create secondary indexes at the time of table creation itself, and you
cannot add indexes to already existing tables. Also, DynamoDB does not allow editing or
deleting indexes from a given table.
DynamoDB supports two types of secondary indexes:
• Local secondary index
• Global secondary index
Search WWH ::




Custom Search