skip to Main Content
  • I have table called details
  • I am trying to get the live count from table
  • Below is the code

I was having already 7 items in the table and I inserted 8 items. Now My output has to show 15.

Still my out showing 7 which is old count. How to get the live updated count

  • From UI also when i check its showing 7 but when i checked live count by Start Scan, I got 15 entries

  • Is there time is there like some hours which will update the live count?

dynamo_resource = boto3.resource('dynamodb')
dynamodb_table_name = dynamo_resource.Table('details')
item_count_table = dynamodb_table_name.item_count
print('table_name count for field is', item_count_table)

using client

dynamoDBClient = boto3.client('dynamodb')
table = dynamoDBClient.describe_table(TableName='details')
print(table['Table']['ItemCount'])

2

Answers


  1. According to the DynamoDB documentation,

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

    You might need to keep track of the item counts yourself if you need to return an updated count shortly after editing the table.

    Login or Signup to reply.
  2. In your example you are calling DynamoDB DescribeTable which only updates its data approximately every 6 hours:

    Item Count:
    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.

    https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_TableDescription.html#DDB-Type-TableDescription-ItemCount

    In order to get the "Live Count" you have two possible options:


    Option 1: Scan the entire table.

    dynamoDBClient = boto3.client('dynamodb')
    item_count = dynamoDBClient.scan(TableName='details', Select='COUNT')
    print(item_count)
    

    Be aware this will call for a full table Scan and may not be the best option for large tables.


    Option 2: Update a counter for each item you write

    You would need to use TransactWriteItems and Update a live counter for every Put which you do to the table. This can be useful, but be aware that you will limit your throughput to 1000 WCU per second as you are focusing all your writes on a single item.

    You can then return the live item count with a simple GetItem.

    https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_TransactWriteItems.html

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search