您如何查询 DynamoDB? [英] How do you query DynamoDB?

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

问题描述

我正在研究 Amazon 的 DynamoDB,因为它看起来消除了维护和扩展数据库服务器的所有麻烦.我目前正在使用 MySQL,维护和扩展数据库是一件非常头疼的事情.

I'm looking at Amazon's DynamoDB as it looks like it takes away all of the hassle of maintaining and scaling your database server. I'm currently using MySQL, and maintaining and scaling the database is a complete headache.

我已经阅读了文档,但我很难理解您将如何构建数据以便轻松检索.

I've gone through the documentation and I'm having a hard time trying to wrap my head around how you would structure your data so it could be easily retrieved.

我对 NoSQL 和非关系型数据库完全陌生.

I'm totally new to NoSQL and non-relational databases.

从 Dynamo 文档看来,您只能在主哈希键和主范围键上查询具有有限数量的比较运算符的表.

From the Dynamo documentation it sounds like you can only query a table on the primary hash key, and the primary range key with a limited number of comparison operators.

或者您可以运行全表扫描并对其应用过滤器.问题是它一次只能扫描 1Mb,因此您可能需要重复扫描才能找到 X 个结果.

Or you can run a full table scan and apply a filter to it. The catch is that it will only scan 1Mb at a time, so you'd likely have to repeat your scan to find X number of results.

我意识到这些限制使它们能够提供可预测的性能,但似乎很难将您的数据取出.并且执行全表扫描似乎会非常低效,而且随着表的增长,效率只会随着时间的推移而降低.

I realize these limitations allow them to provide predictable performance, but it seems like it makes it really difficult to get your data out. And performing full table scans seems like it would be really inefficient, and would only become less efficient over time as your table grows.

例如,假设我有一个 Flickr 克隆.我的图片表可能类似于:

For Instance, say I have a Flickr clone. My Images table might look something like:

  • 图像 ID(数字、主哈希键)
  • 添加日期(数字、主范围键)
  • 用户 ID(字符串)
  • 标签(字符串集)

因此,使用查询我将能够列出过去 7 天的所有图像,并且很容易将其限制为 X 个结果.

So using query I would be able to list all images from the last 7 days and limit it to X number of results pretty easily.

但是,如果我想列出来自特定用户的所有图像,我需要进行全表扫描并按用户名过滤.标签也是如此.

But if I wanted to list all images from a particular user I would need to do a full table scan and filter by username. Same would go for tags.

因为您一次只能扫描 1Mb,您可能需要进行多次扫描才能找到 X 个图像.我也没有看到一种方法可以轻松地停在 X 个图像上.如果您尝试抓取 30 张图像,您的第一次扫描可能会找到 5 张,第二次可能会找到 40 张.

And because you can only scan 1Mb at a time you may need to do multiple scans to find X number of images. I also don't see a way to easily stop at X number of images. If you're trying to grab 30 images, your first scan might find 5, and your second may find 40.

我有这个权利吗?它基本上是一种权衡吗?您可以获得真正快速、可预测的数据库性能,几乎无需维护.但权衡是您需要构建更多逻辑来处理结果?

Do I have this right? Is it basically a trade-off? You get really fast predictable database performance that is virtually maintenance free. But the trade-off is that you need to build way more logic to deal with the results?

或者我完全不在这儿了?

Or am I totally off base here?

推荐答案

是的,您对性能和查询灵活性之间的权衡是正确的.

Yes, you are correct about the trade-off between performance and query flexibility.

但是有一些技巧可以减轻痛苦——二级索引/非规范化可能是最重要的.

But there are a few tricks to reduce the pain - secondary indexes/denormalising probably being the most important.

例如,您将有另一个以用户 ID 为键的表,列出他们的所有图像.当您添加图像时,您会更新此表并在以图像 ID 为键的表中添加一行.

You would have another table keyed on user ID, listing all their images, for example. When you add an image, you update this table as well as adding a row to the table keyed on image ID.

您必须决定需要哪些查询,然后围绕它们设计数据模型.

You have to decide what queries you need, then design the data model around them.

这篇关于您如何查询 DynamoDB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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