Search code examples
iospaginationlimitamazon-dynamodb

AWS dynamoDB limit or pagination, when fetching from unsorted table (ios)


I have an unsorted table (only hash key, without range key).

I must search and fetch limit rows from table. If I use the limit property of AWSDynamoDBScanExpression. But this limit property is not working: I get responses with less than count elements when using the limit property.

I read about pagination - exclusiveStartKey and LastEvaluatedKey properties, but it is good for sorted data. I have unsorted data. And my data must be unsorted and return random rows with limit in every query.

Roughly speaking, I need to start and limit analogs from SQL, but for dynamoDB. It is possible? I do not understand how to do this with a scan operation. Please help me set a limit of fetched rows.


Solution

  • The exclusiveStartKey is the equivalent of offset.

    The DynamoDB API limit is an API limit - not an application limit.

    It doesn't make sure you get <= limit elements. It just forces a single request to return <= limit elements and set the next chunk via LastEvaluatedKey

    To randomize the response, you will have to fetch more events than you might need and randomly select between them.