Consider a DynamoDB table with 25 items. You run a scan with a limit of 10 items per page. The code below fetches the first page and then the second page using the LastEvaluatedKey.
response1 = table.scan(Limit=10)
items_page1 = response1['Items']
last_key = response1.get('LastEvaluatedKey')
response2 = table.scan(Limit=10, ExclusiveStartKey=last_key)
items_page2 = response2['Items']
total_items = len(items_page1) + len(items_page2)What is the value of total_items after running this code?
response1 = table.scan(Limit=10) items_page1 = response1['Items'] last_key = response1.get('LastEvaluatedKey') response2 = table.scan(Limit=10, ExclusiveStartKey=last_key) items_page2 = response2['Items'] total_items = len(items_page1) + len(items_page2)
Each scan call returns up to the limit number of items. The second scan starts after the last key from the first scan.
The first scan returns 10 items. The second scan also returns 10 items starting after the last key of the first scan. So total items fetched are 20.
When paginating scan results in DynamoDB, what role does LastEvaluatedKey play?
Think about how to continue scanning from where you left off.
LastEvaluatedKey tells DynamoDB where to start the next scan to avoid returning duplicate items.
You want to scan all items in a DynamoDB table using pagination. Which code snippet correctly implements this?
Remember to handle the first scan call without ExclusiveStartKey.
Option A correctly handles the first scan without ExclusiveStartKey and uses it for subsequent scans until no LastEvaluatedKey is returned.
You want to paginate scan results but reduce the read capacity units consumed. Which approach helps achieve this?
Think about reducing the amount of data read per item.
Using ProjectionExpression reduces the data returned and read capacity used by only fetching necessary attributes.
Review this code snippet that paginates a DynamoDB scan. It causes an infinite loop. What is the cause?
items = []
last_key = None
while True:
response = table.scan(Limit=10, ExclusiveStartKey=last_key)
items.extend(response['Items'])
if 'LastEvaluatedKey' not in response:
break
# Missing update of last_key hereCheck if the pagination key is updated inside the loop.
The code never updates last_key with LastEvaluatedKey, so the scan always starts at the same place, causing an infinite loop.