Search code examples
pythonamazon-dynamodbboto3

DynamoDB: KeyConditionExpression to check if an attribute exists or not null


I am using boto3 to query DynamoDB. And I have heard that table.query() is more efficient that table.scan() I was wondering if there is a way to check if the value exists using query() method?

response = table.scan(FilterExpression=Attr('attribute').exists()

If it is not possible to check using .query() is there any other method that is more efficient than .scan()?

This question is not a duplicate - I am looking for a way to optimize querying for existing/non existing attributes of .query() or .scan()


Solution

  • It seems that query() is more efficient than scan() because it works with hash key only, and the query must match base table schema. That is the reason for it to be so efficient.

    Therefore it is not possible to scan() for attributes that are not in the base schema.