Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDB query/sort based on timestamp

In DynamoDB, I have a table where each record has two date attributes, create_date and last_modified_date. These dates are in ISO-8601 format e.g. 2016-01-22T16:19:52.464Z.

I need to have a way of querying them based on the create_date and last_modified_date e.g.

  • get all records where create_date > [some_date]
  • get all records where last_modified_date < [some_date]

In general, I need to get all records where [date_attr] [comparison_op] [some_date].

One way of doing it is to insert a dummy fixed attribute with each record and create an index with the dummy attribute as the partition key and the create_date as the sort key (likewise for last_modified_date.)

Then I'll be able to query it as such by providing the fixed dummy attribute as partition key, the date attributes as the sort key and use any comparison operators <, >, <=, >=, and so on.

But this doesn't seem good and looks like a hack instead of a proper solution/design. Are there any better solutions?

like image 707
Shikasta_Kashti Avatar asked Jan 22 '16 16:01

Shikasta_Kashti


People also ask

Does DynamoDB support timestamp?

Amazon DynamoDB TTL allows you to define a per-item timestamp to determine when an item is no longer needed. After the expiration of the TTL timestamp, DynamoDB deletes the item from your table within 48 hours without consuming any write throughput.

How do I sort DynamoDB query results?

DynamoDB uses the partition key as an input to the hash function. The hash function's output decides which partition the item will be placed in, and all items with the same partition key value are stored together. The sort key is used to sort and order items in a partition.

Is DynamoDB good for time series data?

In this post, I show you how to use such an anti-pattern for DynamoDB, but it is a great fit for time-series data. Unless you opt for on-demand capacity mode, every DynamoDB access pattern requires a different allocation of read capacity units and write capacity units.

Can you add a sort key later to DynamoDB?

DynamoDB doesn't allow to add a sort key for an already existing table. Your only option is to create a new table with redefined key attributes and then copy the data from the existing table to the newly created table.


1 Answers

There are some things that NoSQL DBs are not good at, but you can solve this with the following solutions:

  1. Move this table data to SQL database for searching purpose: This can be effective because you will be able to query as per your requirement, this might be tedious sometimes because you need to synchronize the data between two different DBs

  2. Integrate with Amazon CloudSearch: You can integrate this table with CloudSearch and then rather than querying your DynamoDB table you can query Cloudsearch

  3. Integrate with Elasticsearch: Elasticsearch is similar to CloudSearch although each has pros and cons, the end result would be same - rather than querying DynamoDB, instead query Elasticsearch

  4. As you have mentioned in your question, add GSI indexes

like image 57
Harshal Bulsara Avatar answered Oct 24 '22 18:10

Harshal Bulsara