DVA-C02 Question 97
Single answerYou are developing an e-commerce application using Amazon DynamoDB to store product information. The table uses 'ProductID' as the partition key and 'Category' as a sort key. You need to query all products within a specific category. However, you also want to retrieve all products whose price falls within a certain range, regardless of the category. Which approach would best meet these requirements?
- A
Use the existing table with a Query operation on the 'Category' sort key and apply a filter expression for the price range.
- B
Create a Global Secondary Index (GSI) with 'Price' as the partition key and 'Category' as the sort key, and perform a Query operation.
- C
Create a Local Secondary Index (LSI) with 'Price' as the sort key, and perform a Query operation using 'ProductID' as the partition key.
- D
Perform a full table scan using a Scan operation with a filter expression for both 'Category' and 'Price'.
Show answer and explanation
Correct answer: B
Explanation
To efficiently query products within a specific price range regardless of their category, a Global Secondary Index (GSI) is the best solution. GSIs allow you to define an alternative partition key and sort key, enabling you to query by attributes other than the base table's primary key. In this case, using 'Price' as the GSI partition key and 'Category' as the sort key ensures performant queries without scanning the entire table.
- A. Incorrect.
Using a Query operation with a filter expression can retrieve all items in a specific category, but applying a filter for the price range would still require scanning the entire partition, which is inefficient for large datasets.
- B. Correct.
Creating a Global Secondary Index (GSI) with 'Price' as the partition key and 'Category' as the sort key allows for efficient querying by price range, regardless of the category. This meets the requirements without performing a full table scan.
- C. Incorrect.
A Local Secondary Index (LSI) requires the same partition key ('ProductID') as the base table. Since you want to query across all products, 'ProductID' is not suitable. LSIs are not appropriate for this use case.
- D. Incorrect.
Performing a full table scan with a Scan operation is inefficient and not recommended for large datasets. Querying with an index is a better approach for this scenario.