DVA-C02 · Question #503
A company's application includes an Amazon DynamoDB table for product orders. The table has a primary partition key of orderId and has no sort key. The company is adding a new feature that requires th
The correct answer is B. Create a new global secondary index (GSI) on the table with a partition key of customerId.. This question tests understanding of DynamoDB indexing options to enable efficient queries on a non-primary-key attribute.
Question
A company's application includes an Amazon DynamoDB table for product orders. The table has a primary partition key of orderId and has no sort key. The company is adding a new feature that requires the application to query the table by using the customerId attribute. Which solution will provide this query functionality?
Options
- AChange the existing primary key by setting customerId as the sort key.
- BCreate a new global secondary index (GSI) on the table with a partition key of customerId.
- CCreate a new local secondary index (LSI) on the table with a partition key of customerId.
- DCreate a new local secondary index (LSI) on the table with a partition key of orderId and a sort
How the community answered
(31 responses)- A3% (1)
- B81% (25)
- C10% (3)
- D6% (2)
Why each option
This question tests understanding of DynamoDB indexing options to enable efficient queries on a non-primary-key attribute.
The primary partition key of an existing DynamoDB table cannot be changed in place; the table would need to be recreated, and adding a sort key still requires orderId to be provided in queries.
A Global Secondary Index (GSI) lets you define a completely different partition key independent of the table's primary key. Creating a GSI with customerId as the partition key enables efficient queries by customerId across all items in the table without requiring orderId, which is exactly the new query pattern needed.
A Local Secondary Index (LSI) must share the same partition key as the base table (orderId), so customerId cannot serve as its partition key, making LSI unsuitable for this requirement.
An LSI with orderId as partition key and customerId as sort key still requires knowing orderId to query, so it does not support querying by customerId alone.
Concept tested: DynamoDB Global Secondary Index for alternate query access patterns
Source: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html
Community Discussion
No community discussion yet for this question.