Dynamo DB,如何查询所有内容并利用排序键 [英] Dynamo DB, How do you query everything AND leverage sort key

查看:157
本文介绍了Dynamo DB,如何查询所有内容并利用排序键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了一个索引,并将第二个排序键设置为所需的值(整数时间戳记)。该API一直抱怨我没有给它KeyConditionExpression。然后,如果我给它一个,它说必须指定id。我试图强迫它仅使用id<> null来提供所有内容,但它仍然不会这样做。这有可能吗?

I already have an index set up with the second sort key set to what I want (an integer timestamp). The API keeps complaining that I'm not giving it a KeyConditionExpression. Then if I give it one, it says id must be specified. I've tried forcing it to just give me everything using id <> null and it STILL won't do it. Is this even possible?? Maybe its time to get rid of dynamo if it can't do this utterly simple task.

对于上帝的爱,我想做的就是查询整个表格,并使用我的排序键。我本来会在几个小时前用SQL编写的。.

For the love of god, all I'm trying to do is query the entire table AND have it use my sort key. I would have had this going in SQL hours ago..

推荐答案

首先,DynamoDB是一个NOSQL数据库,因此故意不是SQL。也许您不应该期望能够像过去那样执行类似SQL的查询,而对这是两种完全不同的数据库类型(各有优缺点)感到沮丧。

First of all, DynamoDB is a NOSQL database, so it's intentionally NOT SQL. Perhaps you shouldn't expect to be able to perform SQL like queries that you are used to, and be frustrated by the fact that these are two completely different types of databases, each with its strengths and weaknesses.

DynamoDB中的记录使用哈希键进行分区,并且可以选择在每个分区内进行排序。
应该选择哈希键,以便项目尽可能均匀地分布在分区上。分区的使用使DynamoDB具有极高的可伸缩性和快速性。但是,如果您需要对所有项目进行扫描并按已排序的顺序进行排序,则可能是您使用了错误的工具进行工作,或者需要在客户端对项目进行分类

Records in DynamoDB are partitioned using the hash key, and may optionally be sorted within each partition. The hash key should be picked so that items are as evenly distributed over partitions as possible. The use of partitions is what makes DynamoDB extremely scalable and fast. But if what you need is to scan over all your items and get them in sorted order, then you probably either are using the wrong tool for the job, or you need to sort the items on the client side.

扫描操作将简单地遍历所有分区,并从每个分区返回所有项目。此时,这些项目只能在它们各自的分区内排序。

The scan operation will simply go through all partitions, returning all items from each partition. At this point, the items can only be sorted within their respective partition.

例如,考虑将一组数据划分为3个分区:

As an example, consider a set of data being partitioned into 3 partitions:

Partition A                Partition B                Partition B

Sort key                   Sort key                   Sort key
A                          D                          C
C                          E                          K
P                          G                          L

如您所见,您可以轻松查询每个分区并获取其中的项目以排序的顺序。但是,如果您进行扫描,则如果排序顺序是完全确定的,则可能会得到排序为
[A,C,P,D,E,G,C,K,L]的项目。在这一点上,您必须自己对项目进行排序。

As you can see, you can easily query each partition and get the items in it in sorted order. But if you scan, you will probably get items sorted as [A, C, P, D, E, G, C, K, L], if the sort order is at all deterministic. At this point you would have to sort the items yourself.

有时会看到的技巧是对所有对象使用相等值的虚拟哈希键项,就像您在自己的答案中提到的那样。这样,您可以查询 dummy = 1并获得根据排序键排序的项目。但是,这完全破坏了哈希键的目的,因为所有项都将放在同一分区中,因此根本无法扩展表规模。但是,即使您有一个很小的数据集,如果您发现自己使用DynamoDB,也绝对可以。但是,同样,对于一个小的数据集和这样的用例,您可能首先应该使用其他工具,例如RDS。

A "trick" that is sometimes seen is to use a "dummy" hash key with an equal value for all items, like you mentioned in your own answer. This way you can query for "dummy = 1" and get the items sorted according to the sort key. However, this completely defeats the purpose of the hash key as all items will be put in the same partition, thus not making the table scale at all. But if you find yourself using DynamoDB even though you have a really small dataset, by all means it would work. But again, with a small data set and use-cases like this, you should probably be using another tool such as RDS in the first place.

这篇关于Dynamo DB,如何查询所有内容并利用排序键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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