Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find out if a DynamoDB table is empty?

How can I find out if a DynamoDB table contains any items using the .NET SDK?

One option is to do a Scan operation, and check the returned Count. But Scans can be costly for large tables and should be avoided.

like image 266
Taran Avatar asked Apr 28 '17 20:04

Taran


2 Answers

The describe table count does not return real time value. The item count is updated every 6 hours.

The best way is to scan only once without any filter expression and check the count. This may not be costly as you are scanning the table only once and it would not scan the entire table as you don't need to scan recursively to find whether the table has any item.

A single scan returns only 1 MB of data. If the use case requires real time value, this is the best and only option available.

like image 130
notionquest Avatar answered Sep 28 '22 23:09

notionquest


Edit: While the below appears to work fine with small tables on localhost, the docs state

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

so only use DescribeTable if you don't need an accurate, up to date figure.

Original:

It looks like the best way to do this is to use the DescribeTable method on AmazonDynamoDBClient:

AmazonDynamoDBClient client = ...
if (client.DescribeTable("FooTable").Table.ItemCount == 0)
    // do stuff
like image 23
Taran Avatar answered Sep 28 '22 23:09

Taran