DynamoDB Query Operations - Complete Guide
Why DynamoDB Query Operations Are Important
DynamoDB query operations are fundamental to the AWS Developer Associate exam because they represent the most efficient way to retrieve data from DynamoDB tables. Understanding query operations is essential for building performant, cost-effective applications on AWS. Queries consume fewer read capacity units compared to scans, making them critical for optimizing both performance and cost.
What Are DynamoDB Query Operations?
A Query operation in DynamoDB finds items based on primary key values. You must specify the partition key, and optionally, you can provide a sort key condition to further refine results. Queries can only be performed on tables or secondary indexes that have a composite primary key (partition key + sort key).
Key Components:
- Partition Key (Required): You must provide an exact match for the partition key using an equality condition
- Sort Key (Optional): You can use comparison operators like equals, less than, greater than, between, and begins_with
- ProjectionExpression: Specifies which attributes to return
- FilterExpression: Applied after the query, filters results but does not reduce consumed capacity
- KeyConditionExpression: Defines the partition key and optional sort key conditions
How Query Operations Work
1. Specify the Partition Key: The query targets a specific partition using an equality condition on the partition key
2. Optional Sort Key Conditions: Use operators like =, <, >, <=, >=, BETWEEN, and begins_with to narrow results within that partition
3. Data Retrieval: DynamoDB retrieves items that match the key conditions, sorted by sort key value
4. Filter Application: If a FilterExpression is provided, it filters results after retrieval but before returning to the client
5. Pagination: Results exceeding 1MB are paginated using LastEvaluatedKey
Query vs Scan
- Query: Efficient, targets specific partition, uses key conditions
- Scan: Examines every item in the table, consumes more capacity, less efficient
Important Parameters:
- ScanIndexForward: Set to true for ascending order (default), false for descending order by sort key
- Limit: Maximum number of items to evaluate (not necessarily return)
- ConsistentRead: Set to true for strongly consistent reads (default is eventually consistent)
- Select: Determines what to return (ALL_ATTRIBUTES, ALL_PROJECTED_ATTRIBUTES, COUNT, SPECIFIC_ATTRIBUTES)
Secondary Indexes and Queries
- Local Secondary Index (LSI): Same partition key as base table, different sort key. Created at table creation time only
- Global Secondary Index (GSI): Different partition key and sort key. Can be created anytime. Has its own provisioned throughput
Exam Tips: Answering Questions on DynamoDB Query Operations
1. Remember the Partition Key Rule: Queries always require an exact equality condition on the partition key. If a question asks about retrieving data with only a sort key condition, the answer involves a Scan, not a Query.
2. FilterExpression Does Not Save Capacity: When questions mention reducing read capacity consumption, remember that FilterExpression applies after data is read. Use key conditions or GSIs to reduce capacity usage.
3. Sort Key Operators: Know that begins_with works with strings and is commonly tested. The contains operator is NOT valid for KeyConditionExpression, only for FilterExpression.
4. Consistency Models: Default query behavior is eventually consistent. For strongly consistent reads, ConsistentRead must be set to true. Note: GSIs only support eventually consistent reads.
5. Pagination Awareness: If LastEvaluatedKey is present in the response, there are more items to retrieve. The absence of LastEvaluatedKey indicates all items have been returned.
6. Capacity Calculation: One RCU equals one strongly consistent read or two eventually consistent reads for items up to 4KB.
7. Index Selection Questions: If a question describes a query pattern not supported by the table's primary key, look for answers involving creating a GSI with the appropriate key schema.
8. ScanIndexForward: Remember this parameter controls sort order. Setting it to false returns items in descending sort key order, useful for retrieving most recent items first.
9. Projected Attributes: When querying a secondary index, only projected attributes are returned. Queries for non-projected attributes require fetching from the base table.