LSI and GSI help you find data in DynamoDB in different ways. They make searching faster and easier.
LSI vs GSI comparison in DynamoDB
Local Secondary Index (LSI):
- Same partition key as the main table
- Different sort key
Global Secondary Index (GSI):
- Different partition key
- Different sort key (optional)LSI shares the same partition key as the main table but allows a different sort key.
GSI can have a completely different partition key and sort key, giving more flexibility.
LSI example: Partition Key: UserID Sort Key: OrderDate LSI Sort Key: ProductCategory
GSI example: Partition Key: ProductCategory Sort Key: Price
This creates a table with a main key of UserID and OrderID. It adds an LSI to sort orders by product category for each user. It also adds a GSI to find products by category and price.
CREATE TABLE Orders (
UserID STRING,
OrderID STRING,
OrderDate STRING,
ProductCategory STRING,
Price NUMBER,
PRIMARY KEY (UserID, OrderID),
LOCAL SECONDARY INDEX LSI_ProductCategory (
SORT KEY (ProductCategory)
),
GLOBAL SECONDARY INDEX GSI_CategoryPrice (
PARTITION KEY (ProductCategory),
SORT KEY (Price)
)
);LSI can only be created when the table is created; you cannot add it later.
GSI can be added anytime after the table is created.
LSI shares the same partition key, so it uses the same storage and throughput limits.
GSI has its own throughput and storage, so it can scale independently.
LSI uses the same partition key but a different sort key to give alternate sorting.
GSI uses different partition and sort keys to allow flexible queries.
Use LSI for alternate sorting within the same partition; use GSI for different query patterns.
Practice
Solution
Step 1: Analyze LSI key schema
LSI shares the same partition key as the base table but allows a different sort key for alternate sorting within that partition.Step 2: Analyze GSI key schema
GSI allows both partition key and sort key to be different from the base table, enabling flexible query patterns across partitions.Final Answer:
LSI uses the same partition key but a different sort key; GSI uses different partition and sort keys. -> Option AQuick Check:
LSI = same partition key, different sort key; GSI = different keys [OK]
- Confusing which keys LSI and GSI use
- Thinking LSI can have different partition keys
- Assuming GSI uses the same keys as the base table
Solution
Step 1: Check key schema order for GSI
GSI requires a partition key (HASH) and optionally a sort key (RANGE). The partition key must come first in the KeySchema array.Step 2: Validate attribute roles and projection
{ "IndexName": "GSI1", "KeySchema": [ {"AttributeName": "UserId", "KeyType": "HASH"}, {"AttributeName": "OrderDate", "KeyType": "RANGE"} ], "Projection": {"ProjectionType": "ALL"} } correctly defines UserId as HASH and OrderDate as RANGE with ProjectionType ALL, which is valid for GSI.Final Answer:
{\n \"IndexName\": \"GSI1\",\n \"KeySchema\": [\n {\"AttributeName\": \"UserId\", \"KeyType\": \"HASH\"},\n {\"AttributeName\": \"OrderDate\", \"KeyType\": \"RANGE\"}\n ],\n \"Projection\": {\"ProjectionType\": \"ALL\"}\n} -> Option BQuick Check:
GSI KeySchema starts with HASH key [OK]
- Swapping HASH and RANGE key types
- Using LSI name for GSI definition
- Missing required HASH key in KeySchema
Query on LSI where UserId = '123' and OrderStatus = 'Pending'
Solution
Step 1: Recall LSI query keys
LSI uses the same partition key as the base table but allows querying by the alternate sort key, here 'OrderStatus'.Step 2: Analyze query conditions
The query filters on UserId = '123' (partition key) and OrderStatus = 'Pending' (LSI sort key), so it returns matching items sorted by OrderStatus.Final Answer:
Returns all items with UserId '123' and OrderStatus 'Pending' sorted by OrderStatus. -> Option DQuick Check:
LSI query uses same partition key + LSI sort key [OK]
- Confusing sort key names between base and LSI
- Assuming LSI cannot be queried on alternate sort key
- Thinking query returns all UserId items ignoring sort key
Solution
Step 1: Check attribute presence for GSI indexing
GSI indexes only items that have the specified partition and sort key attributes. Missing attributes mean no index entry.Step 2: Understand query behavior on GSI
If items lack 'Category' or 'Price', they won't appear in the GSI, causing queries to return no results despite existing base table items.Final Answer:
The GSI was not properly populated because the base table items lack the 'Category' or 'Price' attributes. -> Option AQuick Check:
Missing GSI keys in items = no query results [OK]
- Assuming GSI queries can't filter on sort key
- Confusing LSI and GSI usage
- Thinking base table keys must be in GSI query
Solution
Step 1: Base table supports UserId-Timestamp query
Queries by 'UserId' sorted by 'Timestamp' use base table keys directly.Step 2: GSI supports Status-OrderDate query
Queries by 'Status' sorted by 'OrderDate' require a different partition key ('Status'), so a GSI with partition key 'Status' and sort key 'OrderDate' is needed.Final Answer:
Use the base table and a GSI with partition key 'Status' and sort key 'OrderDate'. -> Option CQuick Check:
LSI = same partition key, GSI = different partition key [OK]
- Trying to use LSI with different partition key
- Using only base table keys for all queries
- Confusing which index supports which query pattern
Ready to prove it?
Pass the test and this pattern moves unknown
