The Only 2 Query Methods You Need To Learn In DynamoDB

The Only 2 Query Methods You Need To Learn In DynamoDB

DynamoDB has a lot of different queries you can run to fetch data.

If we put aside the Scan method, as it is not recommended most of the time, we are down to the following methods that DynamoDB provides:

  • =
  • <
  • <=
  • >
  • >=
  • exists()
  • not_exists()
  • contains()
  • not_contains()
  • begins_with()
  • BETWEEN

But 99% of the time, you do not want to use 9 out of 11 of these.

You should master the last two only.

Here's why.

The first 5 are equality operators. They come in useful for numbers and sometimes strings but they aren't commonly used and are somewhat straightforward to understand.

The next 4 are FilterExpression methods. They allow you to filter a subset of the data.

So why aren't they recommended?

Because the filtering happens after the data is fetched.

DynamoDB will charge you for the capacity units before the filtering is applied.

Additionally, the latency is the same as without any filtering applied because of that same principle.

Now that we understand this, we can look at the remaining two query methods:

  • begins_with()
  • BETWEEN

The begins_with() method

begins_with() is the basis of data filtering in DynamoDB.

Contrary to the FilterExpression methods, begins_with() offers the capability to filter your data in an efficient manner.

The filtering is done at the initial query so you get no extra latency and are charged for no extra capacity units.

The data you query with begins_with() is the data you get back, nothing more.

Let's see an example of filtering with begins_with().


Say we have an airline database table that stores flights data.

We can store a flight item with the following keys:

pk (partition key): "air canada",
sk (sort key): "2025-02-27#toronto#montreal"        

This flight record stores a flight that belongs to Air Canada and departs on the 28th of February 2025, from Toronto to Montreal.

We can have many different variations of this item.

With this data model we can use the begins_with() method to apply different filters to our query.

We can get all flights that departed (or will depart) in 2025:

KeyConditionExpression: "pk = air canada AND begins_with(sk, '2025')"        

We can get all flights departing in a given month:

KeyConditionExpression: "pk = air canada AND begins_with(sk, '2025-02')"        

Or all flights departing on a given day from Toronto:

KeyConditionExpression: "pk = air canada AND begins_with(sk, '2025-02-27#toronto')"        

That's some efficient filtering.

But what if we want to get all flights for the next 3 months or 3 weeks for example?

That's where the BETWEEN method comes in...

The BETWEEN method

The BETWEEN method allows you to specify a starting string and an ending string and get back all items whose sort key falls between this range.

This enables range filters like dates and other sequence-based values.

From our examples above, we can retrieve all flights that are scheduled over the next 3 months:

KeyConditionExpression: "pk = air canada AND sk BETWEEN '2025-02' AND '2025-05' "        

We can apply the same logic to query for a range of 3 weeks or 3 days.


Conclusion

Mastering these two most important query methods will enable highly efficient and powerful filtering.

The begins_with() and BETWEEN methods are the query methods you need most of the time to apply filtering on your data.

The begins_with() method allows you to filter using a prefix substring, this enables fine-grained filtering of your data, while the BETWEEN method allows you to perform range filtering, such as date or sequential value ranges.


?? My name is Uriel Bitton and I hope you learned something in this edition of Excelling With DynamoDB.

?? You can share the article with your network to help others learn as well.

?? If you're looking for help with DynamoDB, let's have a quick chat here.

?? I hope to see you in the next week's edition!



Ajay Prabandham

Pragmatism better than Astigmatism!

2 天前

Off topic, Uriel Bitton: I've seen several knowledgeable folk on LinkedIn mention that LinkedIn "penalizes" their posts if the text includes links. I don't know exactly how the algorithm imposes the penalty. The technique they resort to, is to include link(s) as comments. I've started doing that too, but it hasn't exactly helped my popularity! ??

要查看或添加评论,请登录

Uriel Bitton的更多文章