Introduction
Scan pagination helps you get all items from a big table in small parts. This avoids slow or heavy requests.
Jump into concepts and practice - no test required
Scan pagination helps you get all items from a big table in small parts. This avoids slow or heavy requests.
Scan operation with ExclusiveStartKey and Limit parameters. Example: Scan( TableName='YourTable', Limit=10, ExclusiveStartKey={ 'PrimaryKey': { 'S': 'LastEvaluatedKeyValue' } } )
Limit controls how many items you get per scan call.
ExclusiveStartKey tells DynamoDB where to continue scanning from.
Scan( TableName='Products', Limit=5 )
Scan( TableName='Products', Limit=5, ExclusiveStartKey={ 'ProductID': { 'S': '123' } } )
This code reads all items from 'ExampleTable' in chunks of 3 using scan pagination. It keeps scanning until no more items remain.
import boto3 client = boto3.client('dynamodb') def scan_all_items(table_name): last_evaluated_key = None all_items = [] while True: if last_evaluated_key: response = client.scan( TableName=table_name, Limit=3, ExclusiveStartKey=last_evaluated_key ) else: response = client.scan( TableName=table_name, Limit=3 ) items = response.get('Items', []) all_items.extend(items) last_evaluated_key = response.get('LastEvaluatedKey') if not last_evaluated_key: break return all_items # Example usage items = scan_all_items('ExampleTable') print(f"Total items fetched: {len(items)}")
Scan reads the whole table, so it can be slow for big tables.
Use pagination to avoid timeouts and large data loads.
Always check for LastEvaluatedKey to know if more data exists.
Scan pagination breaks big scans into small parts.
Use Limit to set chunk size and ExclusiveStartKey to continue.
Keep scanning until LastEvaluatedKey is empty.
Scan pagination in DynamoDB?LastEvaluatedKey from the previous scan is used as ExclusiveStartKey to continue scanning.Limit sets chunk size, not start key. ScanIndexForward is for queries, not scans. StartKey is not a valid parameter.Limit is set to 5?response = table.scan(Limit=5)
items = response['Items']
last_key = response.get('LastEvaluatedKey')
print(len(items), last_key is not None)Limit=5 returns up to 5 items in one scan call, not all 15.LastEvaluatedKey will be present (not None), indicating more data.response = table.scan(Limit=3)
items = response['Items']
while 'LastEvaluatedKey' in response:
response = table.scan(Limit=3)
items.extend(response['Items'])
print(len(items))ExclusiveStartKey, so it always scans from start.ExclusiveStartKey must be set to previous LastEvaluatedKey in each call.Limit=100 and continue using ExclusiveStartKey.Limit=100 and pass ExclusiveStartKey from LastEvaluatedKey until no more keys correctly loops with Limit=100 and uses ExclusiveStartKey from LastEvaluatedKey. Scan once with Limit=1000 and split results in code fetches all at once, risking timeout. Use ScanIndexForward=true with Limit=100 to paginate uses wrong parameter for scan. Set ExclusiveStartKey to null and scan with Limit=100 once scans only once without continuation.