A developer is designing a corporate desk-booking application. The DynamoDB table uses `DeskId` as the partition key and `BookingDate#Slot` (e.g., `2026-08-01#Morning`) as the sort key. The application must support two new access patterns:
1. Retrieve all bookings for a specific employee (`EmployeeId`) sorted by date.
2. Retrieve only the bookings that are currently marked as "PendingApproval" (representing less than of all bookings) to run a daily cleanup cron job.
Which two options should the developer implement to satisfy these requirements with the lowest consumption of Read Capacity Units (RCUs)?
- Create a Global Secondary Index (GSI) with `EmployeeId` as the partition key and `BookingDate#Slot` as the sort key.Cevap
- BCreate a Local Secondary Index (LSI) with `EmployeeId` as the sort key.
- Create a GSI using a sparse attribute `PendingApprovalStatus` (which is only populated when a booking is pending approval) as the partition key.Cevap
- DPerform a `Scan` operation on the base table using a `FilterExpression` to retrieve bookings where the status attribute equals "PendingApproval".
- ECreate a Local Secondary Index (LSI) with `PendingApprovalStatus` as the partition key.
Cevap
Create a Global Secondary Index (GSI) with `EmployeeId` as the partition key and `BookingDate#Slot` as the sort key, and create a GSI using a sparse attribute `PendingApprovalStatus` (which is only populated when a booking is pending approval) as the partition key.
The correct strategy involves two parts. First, to query across different partition keys (desks) by employee ID, a Global Secondary Index (GSI) with the employee ID as the partition key and the booking date/slot as the sort key must be created. Second, to retrieve the small fraction of bookings pending approval, a sparse GSI should be used. In DynamoDB, if an item does not contain the GSI's partition key attribute, it is not indexed. By populating a status attribute only when a booking is pending approval and setting it as the GSI's partition key, the index remains highly compact, and querying it consumes very few RCUs.
Adım Adım Çözüm
Anahtar Kavram
Optimizing DynamoDB queries using Global Secondary Indexes (GSIs) and Sparse Indexes to minimize Read Capacity Unit (RCU) consumption.