pythonamazon-dynamodbdynamodb-queriespynamodb

PynamoDB - Query or count all items on table


Using PynamoDB, anyone else having issues trying to count all items on table? i have a model that i created a table from. I'm trying to use ModelName.count() but i keep getting 0 even though i have some items there. When sending a specific key to ModelName.count(key) i get the correct results, but i want to count all items.

I tried to query all items and count but it seems that i must set the primary key to query, so this walkaround isn't relevant.

Ill be glad for help here if someone dealt with this one before, Thanks!


Solution

  • To count all items in the table without scanning the entire table at a huge cost, pynamodb would have to use the DescribeTable operation, which returns a ItemCount response. However, the DynamoDB documentation explains that:

    DynamoDB updates this value approximately every six hours. Recent changes might not be reflected in this value.

    So it is possible that this is your problem. Please try to wait six hours and see if the count gets updated.

    Regarding querying all the items without a key - you can do that, but it's not called a Query, it's called a Scan. And it will be very expensive (even if you just want to count, you'll basically be paying for reading the entire contents of the database).