Home › Forums › AWS › AWS Certified Developer Associate › Indexes in DynamoDB
-
Indexes in DynamoDB
-
-
Hello Theo,
Thank you for posting here and we’re happy to assist you. Let me clarify the difference between Local Secondary Indexes (LSIs) and Global Secondary Indexes (GSIs).
In Amazon DynamoDB, Local Secondary Indexes (LSIs) and Global Secondary Indexes (GSIs) serve to enhance your querying capabilities but cater to different needs.
Local Secondary Indexes (LSIs) allow you to create indexes that include different sort keys while keeping the same partition key as your base table. For instance, imagine you have a table named Library with a primary key consisting of BookID (partition key) and PublishDate (sort key). You could set up an LSI if you want to query books by AuthorName but still keep BookID as the partition key. This index would enable you to find all books by a specific author within the same partition but sorted by PublishDate.
On the other hand, Global Secondary Indexes (GSIs) offer greater flexibility by allowing you to define entirely different partitions and sort keys from those used in the base table. For the same Library table, suppose you want to efficiently query books by Genre and sort them by PublishDate. You would create a GSI where Genre serves as the partition key and PublishDate as the sort key. This setup lets you query all books of a particular genre and see them ordered by publication date, irrespective of the BookID.
In short, Local Secondary Indexes (LSIs) are useful for adding alternative sorting options while using the same partition key from your base table. They are great for making specific queries within partitions. On the other hand, Global Secondary Indexes (GSIs) provide more flexibility with different partition and sort keys, allowing for a wider range of queries and sorting options across the entire table.
Hope this helps in clarifying the difference between the two.
Regards,
Nikee @ Tutorials Dojo
-
Thank you Nikee for your great explanation.
Kind regards,
Theo
Log in to reply.