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

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

问题描述

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

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)."
";

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

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 网络控制台中不可用,我已经检查过了.(起初它看起来像显示在分页按钮旁边,但事实证明,随着您转到下一页项目,这个数字会变大).

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.

最简单的解决方案是返回 ItemCount 的 DescribeTable.唯一的问题是计数不是最新的.计数每 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天全站免登陆