Secondary indexes

If we need to project even more attributes into an index, we have the ability to create secondary indexes. There are two types of secondary indexes available in DynamoDB:

  • Local secondary index (LSI): A local secondary index gives us the ability to project another sort key into the index and allows for additional data access patterns to be created for our items. A local secondary index supports both strong and consistent reads across the key data and is created locally at the key partition, meaning each key will have a local index created for it. A local secondary index consumes the read and write capacity units of our table. For example, let's say we are tracking our user logins with timestamps in a DynamoDB table. Each time one of our users logs in, they create an entry with their username and a timestamp. We can select the timestamp as the local secondary index, and this way we can filter the login dates according to a time frame that we specify in our query. With a local secondary index, we always need to specify the key in our query as the index is local to the key. 
  • Global secondary index (GSI): With global secondary indexes, we can achieve an even more advanced design of secondary indexes, as the primary key and the sort key of a global secondary index are going to be separate from the partition key and the sort key defined in the table. A global secondary index thus allows us the ability to create a completely different set of attributes with which we can access our data. For example, we can project the timestamp in the global secondary index, and by searching on a particular time range with our query, we will now be able to retrieve not just one user's login times but the times of all users in our table. The global secondary index also has its own read and write capacity units assigned to it, but is only able to retrieve eventually consistent reads from the table.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.185.196