Querying and Scanning DynamoDB From Code
Start course

This course looks at how to read and write data to and from DynamoDB tables. We’ll also touch on performance considerations to allow you to get the most possible performance out of your DynamoDB deployment.

Learning Objectives

  • Reading and Writing from the console
  • Reading and Writing from code
  • Looking for items in your DynamoDB tables

Intended Audience

  • Developers looking to learn how to read and write data to/from your DynamoDB tables


  • Cloud Development
  • AWS Software Development Kit (SDK)
  • DynamoDB Basics

Now, when looking for information concerning your DynamoDB table, should you query or scan?

If you have access to the partition key and the sort key, then of course it's a no-brainer, you're going to be using a query operation. I'll show you how to do both right now. And later on, we're going to talk briefly about performance. But for now, just remember that scanning is a slow inefficient process that you should avoid if possible. Let me show you my lambda here. It's called test-dynamo-read. Let's look up the code. And just like the right operation, we're going to start with the border three resource and we're going to connect to our table using the resource we just created and the table method here, or attribute. Now, once we're connected, we can just call the get item operation for a query. In this case, I'm providing the partition key of the table. And of course, in this case, the sort key as well, which is, last_flight. And I'm providing a couple of values here that I know exist in this table, so that we get a response. Now, moving on to line 13, I have a full table scan. This is obviously something that you really want to avoid, especially if you have hundreds of thousands, if not millions of records in your table. You should never do a full table scan like this.

But I wanted to show you how it's done. It's simply calling the scan method on the table resource that we created. Now, the interesting line here is line 20, which is the same table scan. But in this case, we're passing a filter. So, if we know at least some of the information inside the table. In this case, I'm just interested in subscribers. So, the record needs to have a subscriber attribute, and in this case, it needs to be equal to true for it to be returned. Again, these scan operations will literally go to every record, every row in the table. So, like I said, if you have lots and lots of information, this will take a lot of time and it would be very inefficient, especially if you're trying to do operations involving tons of users accessing your table at the same time and handling lots and lots of data. Let's do an execution here. As you can see, the very first one, we have the query, we get our record. And the next one is the entire table scan. In this case, we get every single row in the table. Of course, in this case, it doesn't matter all that much because we have a very few rows. I believe we have four total records in our table. And finally, we have our same scan operation, but this time with a filter. So, as you can see, this is how this works, it's very, very straightforward and simple.

Just remember that, as long as you have a couple dozen users, it shouldn't matter. You should not need to write super efficient code. This will work just fine for anything that you're doing with your DynamoDB table. But as soon as you start to scale, you need to consider queries over scans because things can get very slow very quickly depending on how much data you are handling.


About the Author
Carlos Rivas
Sr. AWS Content Creator
Learning Paths

Software Development has been my craft for over 2 decades. In recent years, I was introduced to the world of "Infrastructure as Code" and Cloud Computing.
I loved it! -- it re-sparked my interest in staying on the cutting edge of technology.

Colleagues regard me as a mentor and leader in my areas of expertise and also as the person to call when production servers crash and we need the App back online quickly.

My primary skills are:
★ Software Development ( Java, PHP, Python and others )
★ Cloud Computing Design and Implementation
★ DevOps: Continuous Delivery and Integration