Bird
Raised Fist0
DynamoDBquery~20 mins

Local Secondary Index (LSI) concept in DynamoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
LSI Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding the purpose of Local Secondary Index (LSI)

What is the main purpose of a Local Secondary Index (LSI) in DynamoDB?

ATo replicate data across multiple regions automatically
BTo create a global index that spans all partitions with a different partition key
CTo allow querying data using an alternate sort key while keeping the same partition key
DTo increase the write throughput capacity of the table
Attempts:
2 left
💡 Hint

Think about how LSI relates to the partition key and sort key in DynamoDB.

❓ query_result
intermediate
2:00remaining
Query result using LSI with alternate sort key

Given a DynamoDB table with partition key UserID and sort key Timestamp, and an LSI with the same partition key UserID but alternate sort key OrderStatus, what will the following query return?

Query on LSI where UserID = '123' and OrderStatus = 'Pending'
ANo items because LSI cannot be queried
BAll items with UserID '123' regardless of OrderStatus
CAll items with OrderStatus 'Pending' regardless of UserID
DAll items with UserID '123' and OrderStatus 'Pending'
Attempts:
2 left
💡 Hint

Remember that LSI uses the same partition key but a different sort key.

📝 Syntax
advanced
2:00remaining
Identify the correct syntax to create an LSI

Which of the following JSON snippets correctly defines a Local Secondary Index (LSI) when creating a DynamoDB table?

A{ "IndexName": "StatusIndex", "KeySchema": [{"AttributeName": "UserID", "KeyType": "HASH"}, {"AttributeName": "OrderStatus", "KeyType": "RANGE"}], "Projection": {"ProjectionType": "ALL"} }
B{ "IndexName": "StatusIndex", "KeySchema": [{"AttributeName": "OrderStatus", "KeyType": "HASH"}, {"AttributeName": "UserID", "KeyType": "RANGE"}], "Projection": {"ProjectionType": "ALL"} }
C{ "IndexName": "StatusIndex", "KeySchema": [{"AttributeName": "UserID", "KeyType": "HASH"}], "Projection": {"ProjectionType": "ALL"} }
D{ "IndexName": "StatusIndex", "KeySchema": [{"AttributeName": "OrderStatus", "KeyType": "RANGE"}], "Projection": {"ProjectionType": "ALL"} }
Attempts:
2 left
💡 Hint

LSI must use the same partition key as the base table and a different sort key.

❓ optimization
advanced
2:00remaining
Choosing LSI for query performance optimization

You have a DynamoDB table with a partition key CustomerID and sort key OrderDate. You want to efficiently query all orders by CustomerID filtered by OrderStatus. Which approach optimizes query performance?

ACreate a Local Secondary Index with partition key <code>CustomerID</code> and sort key <code>OrderStatus</code>
BCreate a Global Secondary Index with partition key <code>OrderStatus</code> and sort key <code>CustomerID</code>
CScan the entire table and filter results by <code>OrderStatus</code>
DAdd <code>OrderStatus</code> as a new attribute but do not index it
Attempts:
2 left
💡 Hint

Think about how LSI shares the partition key and allows alternate sort keys for efficient queries.

🔧 Debug
expert
2:00remaining
Diagnosing LSI write capacity error

You created a DynamoDB table with an LSI. When writing new items, you receive a ValidationException error stating: "The total size of indexed attributes exceeds the maximum allowed for LSI." What is the cause of this error?

AThe combined size of the base table item and all indexed attributes for the LSI exceeds 10 GB limit
BThe total size of all indexed attributes projected into the LSI exceeds 10 GB per partition key
CThe total size of all indexed attributes projected into the LSI exceeds 10 MB per partition key
Dyek noititrap rep BG 01 sdeecxe ISL eht otni detcejorp setubirtta dexedni lla fo ezis latot ehT
Attempts:
2 left
💡 Hint

Check DynamoDB limits on LSI indexed attribute sizes per partition key.

Practice

(1/5)
1. What is the main purpose of a Local Secondary Index (LSI) in DynamoDB?
easy
A. To store data in a separate table
B. To allow querying data with the same partition key but different sort keys
C. To create a new partition key for faster queries
D. To backup data automatically

Solution

  1. Step 1: Understand LSI functionality

    LSI lets you query items with the same partition key but sorted by an alternate sort key.
  2. Step 2: Compare options

    Only To allow querying data with the same partition key but different sort keys correctly describes this feature; others describe unrelated features.
  3. Final Answer:

    To allow querying data with the same partition key but different sort keys -> Option B
  4. Quick Check:

    LSI = alternate sort key with same partition key [OK]
Hint: LSI changes sort key, not partition key [OK]
Common Mistakes:
  • Thinking LSI creates new partition keys
  • Confusing LSI with Global Secondary Index
  • Believing LSI stores data separately
2. Which of the following is the correct way to define a Local Secondary Index (LSI) when creating a DynamoDB table?
easy
A. Define the LSI with a different partition key than the main table
B. Define the LSI with a new partition key and no sort key
C. Define the LSI after the table is created using updateTable API
D. Define the LSI with the same partition key but a different sort key

Solution

  1. Step 1: Recall LSI definition rules

    LSIs must use the same partition key as the main table but can have a different sort key.
  2. Step 2: Check options

    Only Define the LSI with the same partition key but a different sort key matches this rule; others violate partition key or timing constraints.
  3. Final Answer:

    Define the LSI with the same partition key but a different sort key -> Option D
  4. Quick Check:

    LSI = same partition key + different sort key [OK]
Hint: LSI shares partition key, differs in sort key [OK]
Common Mistakes:
  • Trying to create LSI after table creation
  • Using different partition keys for LSI
  • Omitting sort key in LSI definition
3. Given a DynamoDB table with partition key UserID and sort key Timestamp, and an LSI with sort key OrderStatus, what will the following query return?

Query: KeyConditionExpression = UserID = '123' AND OrderStatus = 'Pending'
medium
A. All items with OrderStatus 'Pending' regardless of UserID
B. All items with UserID '123' sorted by Timestamp
C. All items with UserID '123' and OrderStatus 'Pending' sorted by OrderStatus
D. Syntax error because OrderStatus cannot be used in KeyConditionExpression

Solution

  1. Step 1: Understand query with LSI

    Query on LSI uses same partition key but different sort key (OrderStatus). The query filters UserID = '123' and OrderStatus = 'Pending'.
  2. Step 2: Analyze query result

    It returns items with UserID '123' and OrderStatus 'Pending', sorted by OrderStatus.
  3. Final Answer:

    All items with UserID '123' and OrderStatus 'Pending' sorted by OrderStatus -> Option C
  4. Quick Check:

    Query on LSI filters by partition and LSI sort key [OK]
Hint: LSI query filters by partition key + LSI sort key [OK]
Common Mistakes:
  • Assuming LSI changes partition key
  • Thinking query returns all items with OrderStatus only
  • Believing syntax error occurs using LSI sort key
4. You created a DynamoDB table with an LSI but your query using the LSI's sort key returns no results even though matching items exist. What is the most likely cause?
medium
A. The partition key value in the query is incorrect
B. You used the LSI's partition key instead of the main table's partition key
C. The LSI's sort key attribute is missing in some items
D. The LSI was not defined at table creation time

Solution

  1. Step 1: Check LSI query requirements

    Queries on LSI require the correct partition key value; wrong partition key returns no results.
  2. Step 2: Evaluate options

    The partition key value in the query is incorrect explains no results due to incorrect partition key; other options are less likely or invalid.
  3. Final Answer:

    The partition key value in the query is incorrect -> Option A
  4. Quick Check:

    Correct partition key needed for LSI query [OK]
Hint: Always verify partition key value in LSI queries [OK]
Common Mistakes:
  • Trying to add LSI after table creation
  • Ignoring missing sort key attributes
  • Confusing LSI partition key with main table partition key
5. You have a DynamoDB table with partition key CustomerID and sort key OrderDate. You want to query all orders for a customer sorted by OrderStatus instead of OrderDate. How should you design the Local Secondary Index (LSI) to achieve this?
hard
A. Create an LSI with partition key CustomerID and sort key OrderStatus defined at table creation
B. Create an LSI with partition key OrderStatus and sort key OrderDate after table creation
C. Create a Global Secondary Index (GSI) with partition key CustomerID and sort key OrderStatus
D. Add OrderStatus as a new partition key in the main table

Solution

  1. Step 1: Understand LSI design rules

    LSI must use the same partition key as the main table but can have a different sort key.
  2. Step 2: Apply to problem

    To sort by OrderStatus for the same CustomerID, create an LSI with partition key CustomerID and sort key OrderStatus at table creation.
  3. Step 3: Eliminate wrong options

    Create an LSI with partition key OrderStatus and sort key OrderDate after table creation is invalid because LSI cannot be created after table creation; Create a Global Secondary Index (GSI) with partition key CustomerID and sort key OrderStatus is about GSI, not LSI; Add OrderStatus as a new partition key in the main table changes partition key, which is not allowed.
  4. Final Answer:

    Create an LSI with partition key CustomerID and sort key OrderStatus defined at table creation -> Option A
  5. Quick Check:

    LSI = same partition key + new sort key at creation [OK]
Hint: LSI must be defined at table creation with same partition key [OK]
Common Mistakes:
  • Trying to create LSI after table creation
  • Confusing LSI with GSI
  • Changing partition key in LSI
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard