A Scan operation in Amazon DynamoDB reads every item in a table or a secondary index. By default, a Scan operation returns all of the data attributes for every item in the table or index. You can use the ProjectionExpression parameter so that Scan only returns some of the attributes, rather than all of them.
Scan always returns a result set. If no matching items are found, the result set is empty.
A single Scan request can retrieve a maximum of 1 MB of data. Optionally, DynamoDB can apply a filter expression to this data, narrowing the results before they are returned to the user.
Key Characteristics:
Maximum Items Read:
Reads up to the maximum number of items set by the Limit parameter.
By default, a single Scan operation reads a maximum of 1 MB of data.
Read Capacity Units (RCUs):
Scan operations consume a large number of RCUs because they access every item in the table.
Projection Expression:
You can use the ProjectionExpression parameter to specify the attributes you want to retrieve. This reduces the amount of data returned and can lower the RCU consumption.
Filter Expression:
A filter expression can be provided to refine the results. The filter expression is applied after the Scan operation is complete but before the results are returned to the user.
Sequential Scanning:
By default, Scan operations proceed sequentially.
Parallel Scan:
For faster performance on large tables or secondary indexes, parallel Scan operations can be requested by providing the Segment and TotalSegments parameters. This divides the scan workload across multiple segments and processes them in parallel.
Consistency:
By default, Scan uses eventually consistent reads.
If a consistent copy of the data is needed at the time the Scan begins, the ConsistentRead parameter can be set to true.
You can use the Query API operation in Amazon DynamoDB to find items based on primary key values.
You must provide the name of the partition key attribute and a single value for that attribute. Query returns all items with that partition key value. Optionally, you can provide a sort key attribute and use a comparison operator to refine the search results. It is more efficient than Scan because it narrows down the results based on primary key and sort key attributes.
Key Characteristics:
Primary Key Search:
Queries are performed based on the primary key attribute.
An optional sort key can also be used to refine the results.
Projection Expression:
The ProjectionExpression parameter can be used to specify the attributes to return, which reduces the amount of data retrieved and lowers the RCU consumption.
Sorting:
Results are sorted by the sort key in numeric order by default (ascending order).
ASCII character code values are used for sorting.
The order can be reversed by setting the ScanIndexForward parameter to false.
Consistency:
By default, queries use eventually consistent reads.
Strongly consistent reads can be used by explicitly setting the ConsistentRead parameter to true.
Efficiency:
Query operations are generally more efficient than Scan operations because they are targeted based on the primary key and optional sort key criteria, avoiding a full table scan.
Comparison of Scan and Query
Scan:
Broad and resource-intensive.
Reads all items in a table or secondary index.
Can use filters and projections to refine results.