November 18, 2020 / Nirav Shah
There are two different ways of getting your information out of the database. In this blog, we will see the difference between DynamoDB Scan and Query API calls.
The first question is “What is a Query?”
Query operation finds items in your table based on the Primary Key attribute and distinct value to search for.
If you are looking for an item where the user ID is equal to 121, then you would be going to query the DynamoDB table based on the Primary Key of a user ID and the value will be 121.
And that will select all the attributes for that item. If you were storing the first name, surname, email, etc
Another example is:
If you only wanted to see the user ID and email address instead of all the attributes for that item in the table and always Results are always sorted by the Sort Key.
Suppose they’re numeric, they’re sorted in numeric order, and that by default is in numeric order, like one, two, three, four.
And if they’re ASCII characters, they are also sorted by default in ascending order. And you can reverse the order by setting the ScanIndexForward parameter to false.
By default, all Queries are Eventually Consistent, but you can set them to be Strongly Consistent, but you need to explicitly set the Query to be Strongly Consistent.
Now let’s understand what is Scan….
Aahhh in simple terms, a Scan operation examines every single item in the table. By default, it returns all the data attributes. You can use the ProjectionExpression parameter to refine your scan results to return.and you can also filter the results of the scan once it’s been run.
A query is much more efficient than a Scan because the Scan dumps the entire table, then filters out the unwanted values to provide the desired result.
How to improve performance…
You have to reduce the impact of a query or scan by setting a smaller page size. Which uses fewer read operations.
For example, you can set the page size to return 40 items.
If possible you should try and avoid using scan operations.so you design a table in the way that you can use the Query, Get, or BatchGetItem APIs.
We learned the difference between DynamoDB Scan and Query API calls with example
As a Director of Eternal Web Private Ltd an AWS consulting partner company, Nirav is responsible for its operations. AWS, cloud-computing and digital transformation are some of his favorite topics to talk about. His key focus is to help enterprises adopt technology, to solve their business problem with the right cloud solutions.
Have queries about your AWS project ideas and concepts? Please drop in your project details to discuss with our AWS experts, professionals and consultants.