A developer is designing a backend service for a ride-sharing application that stores ride details in an Amazon DynamoDB table. The base table uses `RideId` as the partition key. Each item contains attributes such as `RiderId`, `DriverId`, `Fare`, `RideDate`, and `Status` (which can be 'Requested', 'Ongoing', or 'Completed').
The application needs to retrieve the most recent completed rides for a specific driver to display on a dashboard.
Which approach is the most efficient and cost-effective to meet these requirements?
- APerform a `Scan` operation on the base table with a filter expression matching the specific `DriverId` and `Status` of 'Completed', then sort the retrieved items by `RideDate` in the application code.
- Create a Global Secondary Index (GSI) with `DriverId` as the partition key and `RideDate` as the sort key. Perform a `Query` operation on the GSI for the specific driver, setting `ScanIndexForward` to `false`, using a filter expression for the completed status, and setting the `Limit` to .Cevap
- CPerform a `Scan` operation on the base table using a filter expression. If the operation throttles and returns a `ProvisionedThroughputExceededException`, increase the provisioned Read Capacity Units (RCUs) of the base table to handle the high volume of scanned items.
- DInitialize the AWS SDK client in the backend code by hardcoding a dedicated IAM user's credentials with read-only access to the DynamoDB table, then perform a parallel `Scan` operation to retrieve the rides.
Cevap
Create a Global Secondary Index (GSI) with `DriverId` as the partition key and `RideDate` as the sort key. Perform a `Query` operation on the GSI for the specific driver, setting `ScanIndexForward` to `false`, using a filter expression for the completed status, and setting the `Limit` to .
The correct approach involves creating a Global Secondary Index (GSI) with the driver identifier as the partition key and the ride date as the sort key. This allows the application to query directly for the specific driver. Setting the sort order parameter to false returns items in descending order, and specifying a limit of 50 restricts the read operation to only the required dataset, keeping costs low and performance high.
Adım Adım Çözüm
Anahtar Kavram
Using Global Secondary Indexes (GSIs) and Query operations to perform optimized lookups on non-key attributes in Amazon DynamoDB.