I have a Workouts
table, and an index ByPlanIdIsCompletedAndCreatedBy
.
The index includes a partition key for planId
and sort key that has the combined fields isComplete#createdBy
.
I wrote the following params for a DynamoDB query operation, that tries to check if there's at least one item on Workouts that meats the following KeyConditionExpression
, the goal is to minimize read consumption but never to paginate to the next page.
{
TableName: 'Workouts',
IndexName: 'ByPlanIdIsCompletedAndCreatedBy',
KeyConditionExpression: 'planId = :planId begins_with(isComplete#createdBy, :sortKey)',
ExpressionAttributeValues: {
':planId': '<some-id>',
':sortKey': 'TRUE#BBBB',
},
Limit: 1
};
Assuming the table has an item that meets the KeyConditionExpression
requirements, can there be a situation where I get empty results from the query, but I get LastEvaluatedKey
? If so, why, and what is the action I need to do to avoid paginating, or extremely reduce the chances for need in pagination?
The simple answer is you'll always get an item should one exist. In extremely rare edge cases there can be occurrences where you get no items, even if one exists, but it's extremely rare.
However, having a Limit=1 will always return a LEK.
I would ignore the edge case as it's likely not something you'll ever witness, but if your concerned, change your Limit to 2. If 2 items fall inside 4KB then there will be no additional cost to read an extra item.