Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nosql/dynamodb hash and range use case

It's my first time using a NoSQL database so I'm really confused. I'd really appreciate any help I can get.

I want to store data comprising announcements in my table. Essentially, each announcement has an ID, a date, and a text.

So for example, an announcement might have ID of 1, date of 2014/02/26, and text of "This is a sample announcement". Newer announcements always have a greater ID value than older announcements, since they are added to the table later.

There are two types of queries I want to run on this table:

  1. I want to retrieve the text of the announcements sorted in order of date.
  2. I want to retrieve the text and dates of the x most recent announcements (say, the 3 most recent announcements).

So I've set up the table with the following attributes: ID (number) as primary key, and date (string) as range

Is this appropriate for what my use cases? And if so, what kind of query/reads/requests/scans/whatever (I'm really confused about the terminology here too) should I be running to accomplish the two types of queries I want to make?

Any help will be very much appreciated. Thanks!

like image 945
K L Avatar asked Dec 19 '22 17:12

K L


1 Answers

You are on the right track.

As far as sorting, DynamoDB will sort by the range key, so date will work but I'd recommend storing it as a number, perhaps milliseconds since the Unix epoch, rather than a String. This will make it trivial to get the announcements in ascending or descending order based on their created date.

See this answer for an overview of local vs global secondary indexes and what capabilities they provide: Optional secondary indexes in DynamoDB

As far as retrieving all items, you would need to perform a scan. Scans are not as efficient as queries, but since all of Dynamo is on SSD's they're still relatively quick. You don't get the single digit millisecond performance with a scan that you get with a query, so if there's a way to associate announcements with a user ID, you might get better performance than with a scan.

Note that you cannot modify the table schema (hash key, range key, and indexes) after you create the table. There are ways to manually migrate a table or import/export it, but the point is that you should think hard about current and future query requirements up front and design the table to support them. It's very easy to add or stop storing non-key or non-item attributes though, which provides nice flexibility.

Finally, try to avoid thinking of Dynamo as relational. With Dynamo, in a lot of cases you may well be better off de normalizing or duplicating some of the data in exchange for fast query performance.

like image 69
rpmartz Avatar answered Jan 02 '23 05:01

rpmartz