DynamoDB:键及其含义 [英] DynamoDB: Keys and what they mean

查看:327
本文介绍了DynamoDB:键及其含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对如何使用DynamoDB表键感到困惑。该文档提到了HASH(似乎也称为分区)键和RANGE(或SORT?)键。我正在尝试将它们与我以前对数据库索引理论的理解大致保持一致。

I'm confused as to how to use DynamoDB table keys. The documentation mentions HASH (which seem to also be referred to as Partition) keys and RANGE (or SORT?) keys. I'm trying to roughly align these with my previous understanding of database indexing theories.

我目前(主要是基于猜测)的理解是,哈希键本质上是主键-它必须是唯一的,并且会自动索引以便快速阅读-RANGE键基本上是您应该应用于计划在其上进行查询的任何其他字段(在类似WHERE或排序的上下文中)的东西。

My current, mostly guess-based understanding is that a HASH key is essentially a primary key - it must be unique and is automatically indexed for fast-reading - and a RANGE key is basically something you should apply to any other field you plan on querying on (either in a WHERE-like or sorting context).

那么,这与引入本地和全球二级索引有些混淆。他们是如何发挥作用的?

This is then somewhat confused by the introductions of Local and Global Secondary Indexes. How do they play into things?

如果有人能以正确的方向推动我,同时考虑到我目前可能是有缺陷的理解来自文档,我会

If anyone could nudge me in the right direction, bearing in mind my current, probably flawed understanding has come from the docs, I'd be super grateful.

谢谢!

推荐答案

基本上是DynamoDB表是根据分区键(也称为哈希键)进行分区的。

Basically, the DynamoDB table is partitioned based on partition key (otherwise called hash key).

1)如果表只有分区键,那么它必须是唯一的。 DynamoDB表的性能很大程度上取决于分区键。好的分区键应该是一个分散的值(不应该像传统系统中的RDBMS主键那样使用序列号作为分区键。

1) If the table has only partition key, then it has to be unique. The DynamoDB table performance based pretty much on the partition key. The good partition key should be a well scattered value (should not have a sequence number as partition key like RDBMS primary key in legacy systems).

2)如果表包含分区键和排序键(否则称为RANGE键),则它们的组合需要唯一。它是RDBMS术语中的一种级联密钥。

2) If the table has both partition key and sort key (otherwise called RANGE key), then the combination of them needs to be unique. It is a kind of concatenation key in RDBMS terms.

但是,DynamoDB表的用法有所不同。 DynamoDB在分区键上没有排序功能(即ORDER BY子句)。例如,如果您有10个项目具有相同的分区键值和不同的排序键值,则可以基于排序键属性对结果进行排序。您不能对包括分区键在内的任何其他属性应用排序。

However, the usage differs in DynamoDB table. DynamoDB doesn't have a sorting functionality (i.e. ORDER BY clause) across the partition keys. For example, if you have 10 items with same partition key value and different sort key values, then you can sort the result based on the sort key attribute. You can't apply sorting on any other attributes including partition key.

分区键的所有排序键值都将保留在同一分区中,以提高性能(即物理上并置)。

All sort key values of a partition key will be maintained in the same partition for better performance (i.e. physically co-located).

LSI -该表只能有一个LSI。创建表时应定义它。这是表格的另一种排序键

LSI - There can be only one LSI for the table. It should be defined when you create the table. This is kind of alternate sort key for the table

GSI -为了了解GSI,您需要了解 DynamoDB中的SCAN和QUERY API

GSI - In order to understand GSI, you need to understand the difference between SCAN and QUERY API in DynamoDB.

SCAN -在您不知道分区键(即完整表)时使用扫描以获取项目)

SCAN - is used when you don't know the partition key (i.e. full table scan to get the item)

查询-在您知道分区键(即排序键是可选的)时使用

QUERY - is used when you know the partition key (i.e. sort key is optional)

由于DynamoDB成本核算基于读/写容量单位,为了获得更好的性能,不是扫描是大多数情况下的最佳选择用例。因此,有一个选项可基于查询访问模式(QAP)使用备用分区键创建GSI。

As DynamoDB costing is based on read/write capacity units and for better performance, scan is not the best option for most of the use cases. So, there is an option to create the GSI with alternate partition keys based on the Query Access Pattern (QAP).

GSI例子

这篇关于DynamoDB:键及其含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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