Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying DynamoDB with a partition key and list of specific sort keys

I have a DyanmoDB table that for the sake of this question looks like this:

  • id (String partition key)
  • origin (String sort key)

I want to query the table for a subset of origins under a specific id. From my understanding, the only operator DynamoDB allows on sort keys in a Query are 'between', 'begins_with', '=', '<=' and '>='.

The problem is that my query needs a form of 'CONTAINS' because the 'origins' list is not necessarily ordered (for a between operator).

If this was SQL it would be something like:

SELECT * from Table where id={id} AND origin IN {origin_list}

My exact question is: What do I need to do to achieve this functionality in the most efficient way? should I change my table structure? maybe add a GSI? Open to suggestions.

I am aware that this can be achieved with a Scan operation but I want to have an efficient query. Same goes for BatchGetItem, I would rather avoid that functionality unless absolutely necessary.

Thanks

like image 785
Chaos Monkey Avatar asked May 22 '18 13:05

Chaos Monkey


People also ask

Can I query DynamoDB by sort key?

You can not query only using a Sort Key. You need to specify a partition key to perform query operations. Else, you need to create a global secondary index or perform a scan operation.

How do I sort DynamoDB query results?

Query results are always sorted by the sort key value. If the data type of the sort key is Number, the results are returned in numeric order; otherwise, the results are returned in order of UTF-8 bytes. By default, the sort order is ascending. To reverse the order, set the ScanIndexForward parameter to false.

Is there a way to query multiple hash keys in DynamoDB?

There is no way to query by multiple hash keys, but, as of April 2014, you can use QueryFilter so you can filter by non key fields in addition to hash key fields.

What is sort key and partition key in DynamoDB?

Partition key and sort key – Referred to as a composite primary key, this type of key is composed of two attributes. The first attribute is the partition key, and the second attribute is the sort key. DynamoDB uses the partition key value as input to an internal hash function.


1 Answers

This is a case for using Filter Expressions for Query. It has IN operator

Comparison Operator

a IN (b, c, d) — true if a is equal to any value in the list — for example, any of b, c or d. The list can contain up to 100 values, separated by commas.

However, you cannot use condition expressions on key attributes.

Filter Expressions for Query

A filter expression cannot contain partition key or sort key attributes. You need to specify those attributes in the key condition expression, not the filter expression.

So, what you could do is to use origin not as a sort key (or duplicate it with another attribute) to filter it after the query. Of course filter first reads all the items has that 'id' and filters later which consumes read capacity and less efficient but there is no other way to query that otherwise. Depending on your item sizes and query frequency and estimated number of returned items BatchGetItem could be a better choice.

like image 182
Can Sahin Avatar answered Nov 16 '22 02:11

Can Sahin