How to get the row count of a table instantly in DynamoDB? How to get the row count of a table instantly in DynamoDB? python python

How to get the row count of a table instantly in DynamoDB?


There are two ways you can get a row count in DynamoDB.

The first is performing a full table scan and counting the rows as you go. For a table of any reasonable size this is generally a horrible idea as it will consume all of your provisioned read throughput.

The other way is to use the Describe Table request to get an estimate of the number of rows in the table. This will return instantly, but will only be updated periodically per the AWS documentation.

The number of items in the specified index. DynamoDB updates this value approximately every six hours. Recent changes might not be reflected in this value.


As per documentation boto3

"The number of items in the specified table. DynamoDB updates this value approximately every six hours. Recent changes might not be reflected in this value."

import boto3dynamoDBResource = boto3.resource('dynamodb')table = dynamoDBResource.Table('tableName')print(table.item_count)

or you can use DescribeTable:

import boto3dynamoDBClient = boto3.client('dynamodb')table = dynamoDBClient.describe_table(    TableName='tableName')print(table)


If you want to count the number of items:

import boto3client = boto3.client('dynamodb','us-east-1')response = client.describe_table(TableName='test')print(response['Table']['ItemCount'])#ItemCount (integer) --The number of items in the specified table.# DynamoDB updates this value approximately every six hours.# Recent changes might not be reflected in this value.

Ref: Boto3 Documentation (under ItemCount in describe_table())