在DynamoDB可选的辅助指标可选、指标、DynamoDB

2023-09-11 23:51:04 作者:风雨哈佛路

我迁移我的持久层从Riak到DynamoDB。我的数据模型包含一个可选的业务标识符字段,这是所希望的,以便能够被查询以替代密钥。

I am migrating my persistence tier from Riak to DynamoDB. My data model contains an optional business identifier field, which is desired to be able to be queried as an alternative to the key.

看来,DynamoDB二级指标不能键,需要一系列关键的,所以尽管类似的名字,以Riak的二级指标,使这个看起来完全不同的野兽

It appears that DynamoDB secondary indexes can't be null and require a range key, so despite the similar name to Riak's secondary indexes, make this appear quite a different beast.

有一种优雅的方式来有效地查询我的可选字段,短投掷数据在外部搜索索引?

Is there an elegant way to efficiently query my optional field, short of throwing the data in an external search index?

推荐答案

当你问到这个问题,DynamoDB没有全局辅助索引:http://aws.amazon.com/about-aws/whats-new/2013/12/12/announcing-amazon-dynamodb-global-secondary-indexes/

When you asked this question, DynamoDB did not have Global Secondary Indexes: http://aws.amazon.com/about-aws/whats-new/2013/12/12/announcing-amazon-dynamodb-global-secondary-indexes/

现在,它的作用。

本地二级索引是最好的思想,和功能,辅助范围键。 @andreimarinescu是正确的:你还是必须按项目的哈希键查询,只能用二级索引可以使用DynamoDB查询的比较操作的一个有限的子集在该范围键(如大于,等于,小于等)所以,你还需要知道哪些散列桶正在执行中的比较。

A local secondary index is best thought of, and functions as, a secondary range key. @andreimarinescu is right: you still must query by the item's hash key, only with a secondary index you can use a limited subset of a DynamoDB query's comparison operators on that range key (e.g. greater than, equal to, less than, etc.) So, you still need to know which "hash bucket" you're performing the comparison within.

全局二级索引是有点不同的野兽。他们更喜欢你的表的次要版本(和亚马逊会收取同样的配置的吞吐量方面)。您可以在一个全球性的辅助索引使用表作为索引的主键属性的非主键的属性,并相应地对它们进行查询。

Global secondary indexes are a bit of a different beast. They are more like a secondary version of your table (and Amazon charges you similarly in terms of provisioned throughput). You can use non-primary key attributes of your table as primary key attributes of your index in a global secondary index, and query them accordingly.

例如,如果你的表是这样的:

For example, if your table looks like:

|**Hash key**: Item ID | **Range Key**: Serial No | **Attribute**: Business ID |
--------------------------------------------------------------------------------
|           1          |        12345             |             1A             |
--------------------------------------------------------------------------------    
|           2          |        45678             |             2B             |
-------------------------------------------------------------------------------- 
|           3          |        34567             |            (empty)         |
--------------------------------------------------------------------------------
|           3          |        12345             |             2B             |
--------------------------------------------------------------------------------

随后,对业务ID ,你可以执行像找到所有与 3 和商务ID等于 2B ,但你不能这样做找到一个企业ID等于所有项目 2B 因为二级索引需要的哈希键。

Then, with a local secondary index on Business ID you could perform queries like, "find all the items with a hash key of 3 and a business ID equal to 2B", but you could not do "find all items with a business ID equal to 2B" because the secondary index requires a hash key.

如果您要添加使用业务ID的全局二级索引,那么你可以执行这样的查询。你将基本上被提供一个备用主键表。您可以执行像找到所有项目与业务ID等于 2B ,并得到物品查询 2-45678 3-12345 作为响应。

If you were to add a global secondary index using business ID, then you could perform such queries. You would essentially be providing an alternate primary key for the table. You could perform a query like "find all items with a business ID equal to 2B and get items 2-45678 and 3-12345 as a response.

稀疏索引的正常工作与DynamoDB;这是完全允许的,并不是所有的项目都有一个业务ID,可以让你保持的供应吞吐量索引比表更低,这取决于你预计会有一个企业ID多少个项目。

Sparse indexes work fine with DynamoDB; it's perfectly allowable that not all the items have a business ID and can allow you to keep the provisioned throughput on your index lower than that of the table depending on how many items you anticipate having a business ID.