Search code examples
amazon-web-serviceshashamazon-dynamodbdynamodb-queries

DynamoDB Query with FilterExpression not returns the expected data on limit


Here is KeyConditionExpression, FilterExpression, Limit values that I use to query data

KeyConditionExpression (pk = 1122 and begins_with(sk, 'ID#')
FilterExpression (code = alex@001)
Limit 1

Here is the demo Table that contain items and when I query so I don't get any result

pk sk code
1010 ID#01
1010 ID#02
1010 ID#03
1010 ID#04 alex@000
1010 ID#05 alex@001
1010 ID#06 alex@001

What I had originally assumed was that Dynamodb would query the data first, filter it, and then apply limits, but if this was the case, why am I getting no response?

There are no sources that I can discover that explicitly define this behavior.

If enable the Scan Index Forward its works as its just change the sorting.


Solution

  • No DynamoDB first queries the data based on the KeyconditionExpression, applies the limit, and then filters that data and returns the matches.

    So in your case you get no data because you only read 1 item, and it does not contain alex@001.

    In reverse, the single item you read does contain the value, so it gets returned.

    A single Query operation will read up to the maximum number of items set (if using the Limit parameter) or a maximum of 1 MB of data and then apply any filtering to the results using FilterExpression

    This is explained more thoroughly in the Query docs:

    https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html