AWS Certified Developer - Associate
Databases
DynamoDB Indexes GSI LSI
This article explains how DynamoDB leverages Global Secondary Indexes (GSIs) and Local Secondary Indexes (LSIs) to overcome query limitations. By understanding these indexing strategies, you can optimize your DynamoDB queries for efficient data retrieval.
Example Table: Product Reviews
Consider a table that stores product reviews with the following attributes:
- Product ID: The identifier of the reviewed product.
- User: The person who wrote the review.
- Rating: The score assigned to the product.
- Content: The text of the review.
- Created_at: The timestamp indicating when the review was created.
This table uses a composite primary key comprising the partition key (product ID) and the sort key (user), allowing a single user to review multiple products and each product to have reviews from different users.
Querying the Reviews
Retrieve All Reviews for a Given Product
To fetch every review for a product with an ID of 9999, query based solely on the partition key:
Since the query relies on the product ID, it is fast and efficient.
Retrieve a Specific User's Review
To obtain a review submitted by a specific user (for example, Sam) for product 9999, follow these steps:
- Query the partition key (product ID equals 9999).
- Narrow the results by filtering on the sort key (user equals [email protected]).
This method efficiently targets the desired review by using both keys.
Retrieve Reviews with Specific Attributes
Suppose you need all five-star reviews for product 9999. First, query by the partition key (product_id equals 9999) to retrieve all reviews. Then, filter the results client-side to extract the five-star reviews:
Note
Since the filtering by rating occurs on the client side and the rating attribute is not part of the table's primary key, this method can be inefficient when dealing with a large number of reviews.
Retrieve All Reviews by a Specific User
Directly querying on the sort key (user) is not supported unless it is part of the primary key. To query all reviews made by Sam, you must:
- Scan the entire table.
- Filter the results where the user equals [email protected].
This approach is inefficient because scanning reads every item in the table.
Warning
Efficient querying in DynamoDB depends on leveraging the partition key and, optionally, the sort key. Avoid client-side filtering when possible to improve performance.
Local Secondary Index (LSI)
LSIs enable you to use an alternative sort key while keeping the original partition key. For the reviews table, even though the primary sort key is the user, an LSI can allow querying based on the rating attribute.
For example, define an LSI on the "rating" attribute so that you can query:
- Partition key: product_id equals 9999
- Sort key (via LSI): rating equals 5
This design provides an efficient means to query by rating without resorting to client-side filtering.
Key Points About LSIs
- LSIs must be defined during table creation; they cannot be added later.
- A table supports up to five LSIs.
- LSIs share the table’s provisioned read and write capacity units (RCUs and WCUs).
Global Secondary Index (GSI)
GSIs allow you to create a completely new primary key configuration that consists of:
- A new partition key.
- An optional sort key.
This is especially useful when you need to query based on an attribute not included in the main table's primary key.
For example, to query all reviews written by Sam (regardless of product), you can create a GSI with:
- Partition key: user (allowing direct queries for user equals [email protected])
- Optional sort key: for example, rating, to further refine results to five-star reviews.
Benefits of Using GSIs
- GSIs can be added or modified after the table has been created.
- They require separate provisioning of RCUs and WCUs.
- If the GSI write throughput is throttled, it may also throttle writes on the main table.
Summary
Local Secondary Index (LSI):
- Allows an alternative sort key while maintaining the same partition key.
- Must be defined at table creation.
- Supports up to five LSIs per table.
- Shares provisioned capacity with the main table.
Global Secondary Index (GSI):
- Enables creation of a new primary key with its own partition key and optional sort key.
- Can be added or modified after table creation.
- Has separate provisioning for RCUs and WCUs.
- Write throttling on a GSI can impact the main table's performance.
Understanding these indexing options is crucial for optimizing your DynamoDB queries, ensuring efficient data retrieval, and maintaining high performance for your applications.
Watch Video
Watch video content