如何获取DynamoDB表中的项目总数? [英] How can I get the total number of items in a DynamoDB table?

查看:788
本文介绍了如何获取DynamoDB表中的项目总数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我的dynamodb表中有多少个项目.从API指南中,一种方法是使用扫描作为如下:

I want to know how many items are in my dynamodb table. From the API guide, one way to do it is using a scan as follows:

<?php
$dynamodb = new AmazonDynamoDB();

$scan_response = $dynamodb->scan(array(
    'TableName' => 'ProductCatalog' 
));

echo "Total number of items: ".count($scan_response->body->Items)."\n";

但是,这必须获取所有项目并将它们存储在内存中的数组中,这在我猜想的大多数情况下是不可行的.有没有一种方法可以更有效地获取项目总数?

However, this has to fetch all items and store them in an array in memory which isn't feasible in most cases I would presume. Is there a way to get the total item count more efficiently?

我已经检查过此数据在AWS Dynamo Web控制台中不可用. (起初看起来好像是在分页按钮旁边显示的,但是事实证明,随着您转到下一页的项目,图形会增加).

This data is not available in the AWS Dynamo web-console, I have already checked. (at first it looks like it is shown alongside the pagination buttons, but it turns out the figure grows as you go to the next page of items).

推荐答案

我可以想到三个选项来获取DynamoDB表中的项目总数.

I can think of three options to get the total number of items in a DynamoDB table.

  1. 第一个选项是使用扫描,但是扫描功能效率低下,通常是一种不好的做法,尤其是对于读取量大的表或生产表.

  1. The first option is using the scan, but the scan function is inefficient and is in general a bad practice, especially for tables with heavy reads or production tables.

第二种选择是Atharva提到的:

The second option is what was mention by Atharva:

我想到的一个更好的解决方案是保持总数 此类表格在单独表格中的项目计数数量,其中每个 项将具有表名作为其哈希键和项总数 在该表中,因为它是非关键属性.然后,您可以保留此表 可能名为"TotalNumberOfItemsPerTable"的方法是通过原子化来更新 更新操作以增加/减少商品的总计数 特定的表格.

A better solution that comes to my mind is to maintain the total number of item counts for such tables in a separate table, where each item will have Table name as it's hash key and total number of items in that table as it's non-key attribute. You can then keep this Table possibly named "TotalNumberOfItemsPerTable" updated by making atomic update operations to increment/decrement the total item count for a particular table.

唯一的问题是增量运算不是幂等的.因此,如果一次写入失败或您多次写入,这将反映在计数中.如果需要精确度,请使用条件更新.

The only problem this is that increment operations are not idempotent. So if a write fails or you write more than once this will be reflected in the count. If you need pin-point accuracy, use a conditional update instead.

最简单的解决方案是DescribeTable,它返回ItemCount.唯一的问题是该计数不是最新的.计数每6小时更新一次.

The simplest solution is the DescribeTable which returns ItemCount. The only issue is that the count isn't up to date. The count is updated every 6 hours.

http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_DescribeTable .html

这篇关于如何获取DynamoDB表中的项目总数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆