For efficient access to data in a table, DynamoDB creates and maintains indexes for the primary key attributes. You can also create one or more secondary indexes on a table.
Can query the secondary index just as you would query a table. Secondary indexes are also specified with a partition key and a sort key.
When you create a secondary index, you define the alternate key for the index, along with any other attributes that you want to project in the index. DynamoDB copies these attributes into the index, along with the primary key attributes from the table.
Secondary indexes are maintained as sparse objects. Items will only appear in an index if they exist in the table. Hence, you are not charged anything specific to an index if an item without index keys are added to table. Also, the number of items in the index will often be significantly less than the number of items in the table.
Two types of Secondary Indexes
-
Local secondary index (LSI)
-
LSI scoped to a base table partition that has the same partition key value.
-
You can only create local secondary indexes during table creation.
-
-
Global secondary index (GSI)
-
With GSI, queries on the index can span all of the data in the base table, across all partitions.
-
Can be created anytime from the Indexes tab of the table. They cannot be created or modified later.
-
Comparing GSI and LSI (Exam Tips)
-
GSIs associated with a table can be created/updated/deleted at any time. LSI can only be created during table creation; cannot be updated/deleted later.
-
LSIs limit the total size of all elements (tables and indexes) to 10 GB per partition key value. GSIs do not enforce data co-location, and have no such restriction.
-
When a read query requests attributes that are not projected into the LSI, DynamoDB will fetch those attributes from the primary index. A query on a GSI can only return attributes that were specified to be included (projected) in the GSI at creation time.
-
LSIs are updated automatically when the primary index is updated (additions, updates and deletes). GSIs are also updated, but asynchronously.
-
LSI supports both strong and eventually consistent read options. GSIs support only eventual consistency as it is updated only asynchronously.
-
You don’t need to explicitly provision capacity for a LSI. It consumes provisioned capacity as part of the table with which it is associated. GSIs manage throughput independently of the table they are based on.
Limits on Indexes (Common Limits)
- You can create upto 5 GSI and 5 LSI.
-
You can project a total of up to 20 attributes into all of a table's local and global secondary indexes.
-
This only applies to user-specified projected attributes.
-
In a CreateTable operation, if you specify a ProjectionType of INCLUDE, the total count of attributes specified in NonKeyAttributes, summed across all of the secondary indexes, must not exceed 20.
-
If you project the same attribute name into two different indexes, this counts as two distinct attributes when determining the total.
-
This limit does not apply for secondary indexes with a ProjectionType of KEYS_ONLY or ALL.
-
Note: Also see pages for LSI and GSI.
Additional Points (Exam Tips)
-
Redundant copies of data attributes are copied into the indexes.
-
These attributes include the table partition and sort key, plus the index keys you define (partition-key and/or sort-key for GSI and only alternate sort key for LSI) and the projected attributes (attributes that you select to copy to the indexes).
-
This makes queries against the indexes more efficient as you are not involving the original table.
-
-
All scalar data types (Number, String, Binary, and Boolean) can be used as index keys.
-
Set, list, and map types cannot be indexed.
-
Composite attribute indexes are not possible. But you can concatenate attributes into a string and use this as a key.
-
All data types (including set types) can be projected (copied to index) into a GSI/LSI.
-
You can create a maximum of 5 GSI and 5 LSI per table.
-
The local version of DynamoDB (used for developing and testing) support GSIs.
-
You can request a notification to be sent to your email address confirming that the index addition has been completed. When the index creation is complete, DynamoDB will send an SNS notification to email.
- heartin's blog
- Log in or register to post comments
Recent comments