Search code examples
amazon-dynamodbdynamodb-queries

Multi partition key search operation in DynamoDB


Is there some operation of the Scan API or the Query API that allows to perform a lookup on a table with a composite key (pk/sk) but that varies only in the pk to optimize the Scan operation of the table ?

Let me introduce a use case:

Suppose I have a partition key defined by the id of a project and within each project I have a huge amount of records (sk)

Now, I need to solve the query "return all projects". So I don't have a partition key and I have to perform a scan.

I know that I could create a GSI that solves this problem, but let's assume that this is not the case.

Is there any way to perform a scan that "hops" between each pk, ignoring the elements of the sk's?

In other words, I will collect the information of the first record of each partition key.


Solution

  • DynamoDB is a NoSQL database, as you already know. It is optimized for LOOKUP, and practices that you used to have in SQL databases or other (low-scale) databases are not always available in DynamoDB.

    The concept of a partition key is to put records that are part of the same partition together and sorted by the sort key. The other side of it is that records that don't have the same partition key, are stored in other locations. It is not a long list (or tree) of records that you can scan over.

    When you design your schema in a NoSQL database, you need to consider the access pattern to that data. If you need a list of all the projects, you need to maintain an index that will allow it.