DynamoDB 中的可选二级索引 [英] Optional secondary indexes in DynamoDB

查看:17
本文介绍了DynamoDB 中的可选二级索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的持久层从 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 二级索引不能为 null 并且需要范围键,因此尽管名称与 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             |
--------------------------------------------------------------------------------

然后,使用 Business 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-456783-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.

这篇关于DynamoDB 中的可选二级索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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