Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query in Dynamo DB without hashkey or scan

I am looking to query on Dynamo DB without HashKey. I have tried using the scan but it is expensive so looking for some other alternatives.

like image 238
Mridul Avatar asked Mar 08 '16 07:03

Mridul


2 Answers

I should start by saying that querying a DynamoDB table without knowing the hash key can't be done. And it makes sense.

Now, whether the hash key you want to use is the primary key of the table or not, is up to you.

Suppose, for instance, that you have the following table:

╔══════════════════════╦════════════════════════╦════════════════╗
║ course_id (Hash Key) ║      course_name       ║    teacher     ║
╠══════════════════════╬════════════════════════╬════════════════╣
║ 324234               ║ Node.js for Dummies    ║ Ryan Dahl      ║
║ 213323               ║ How to train your cat  ║ Jackson Galaxy ║
║ 324090               ║ Cat Logic              ║ Jackson Galaxy ║
║ 763298               ║ Diving into .NET       ║ Eric Lippert   ║
╚══════════════════════╩════════════════════════╩════════════════╝

The table's primary key and hash key is course_id, which is fine. Providing unique hash keys allows your table to be split into multiple partitions.

But, what if we want to get All the courses that Jackson Galaxy is teaching?

We don't know the course_id of those courses, that's what we want to get. So we find ourselves without knowing the items' hash key values.

That's where GSI come into play. Global Secondary Indexes let you define a different hash key for your table. Note that it will not change the primary hash key - course_id will still be the table's hash key.

GSI only provides an additional hash key for you to be able to make more complex queries.

Let's say we add a GSI named teacher_index, and we say that teacher will be our hash key, and course_id will be our range key (We need to specify a range key, because teacher alone as a hash key will generate duplicate entries).

Now we can query our teacher_index and pass in Jackson Galaxy as a hash key value. The results would be 213323 - How to train your cat and 324090 - Cat Logic.

like image 162
Matias Cicero Avatar answered Sep 20 '22 23:09

Matias Cicero


I partially agree with the answer posted by Matias. You can create a Global Secondary Index on the table (with a hash key and an optional range key).

However, I don't agree with what he says in his answer later: "We need to specify a range key, because teacher alone as a hash key will generate duplicate entries". I can't comment below his answer because I do not have enough reputation. For some reason, I am not able to edit his answer.

From the AWS docs:

Global Secondary indices do not enforce uniqueness

Hence, range keys for Global Secondary Indices (GSI's) are optional and the composite key for a GSI need not uniquely identify any record.

like image 42
ritz777 Avatar answered Sep 19 '22 23:09

ritz777