I am trying to retrieve a fixed number(lets take it as 5 for now) of items from a dynamo db table.
This is the code I am using.
response = table.query(
KeyConditionExpression=Key('pk').eq('goo'),
Limit=5,
FilterExpression=Attr('goo').eq('bar'))
I am getting only 4 items from this. But if I remove FilterExpression, the item count will be 5. So is there any other way to get the fixed number of item even if I am using FilterExpression?
2
Answers
This is the answer I found.
link for paginator doc : DynamoDB.Paginator.Query
Filter Expressions are applied after items are read from the table in order to reduce the number of records sent over the wire. The Limit is applied during the query operation, i.e. before the filter expression.
If the
Query
read 5 items and only 4 of them match theFilterExpression
you’re getting only 4 items back.The pragmatic thing would be to remove the limit from the
Query
and apply the limit client-side. The drawback is that you may pay for more Read Capacity Units.If you want to avoid that you may have to reconsider your datamodel – a generic solution is difficult here.
In your specific case, you could create a Global Secondary Index with the partition key
pk
and the sort keygoo
(it doesn’t have to be unique for GSIs). You can then fire your Query against the GSI with Limit 5 and it will give you what you want. But: you pay for the GSI storage + throughput.Edit: This question is pretty much a duplicate except for the Python code