store and retrieving data in dynamo database(DB) using scan and query

Ganeshrp
5 min readFeb 14, 2021

--

Hello everyone, I am gonna share my experience here on how to extract the data from dynamo database efficiently using scan and query with good performance, use cases and examples with syntax. Wow here we go..

Before that lets see what is dynamo database and how many possible ways to pull the data from dynamo database with simple explanation.

DynamoDB is a fully managed NoSQL database which supports simple key-value pairs and it is partitioned by keys.

In the DynamoDB there are 2 possible ways to pull the items from the database that is scan and query.

Scan

scan operation always scans the entire table. so it takes more time and space to process the data operations such as read, write and delete in DynamoDB. Here you can ask the question when to use the scan for retrieving the data, so you can use the scan method when you have small table structure.

DynamoDB with candidate table with list of items

How to use Scan in DynamoDB ?

Now click the scan text below the create item button from the above image.

Scan/Query

Now you can see the two dropdowns and Add filter button, now press the Add filter button.

Scan filtering is simple you can use any column(key, sort, index sort or simple column) and have multiple filters in the above image.

Scan Results

As you can see in the above image I have filtered with the column name email and professional using scan.

Note: Scan operation does not require Partition Key or Sort key to retrieve the items from the table.

Scan syntax for above example in code level:

Scan syntax

In the above image Model is the schema you defined in the project.

Scan Postman Results

In the above image you can see the Scanned Count is 8, this means it scans all the 8 items in the table and retrieves one item matching to the scan that is why when you have larger table structure it takes more time for retrieving the data. For example you have 10,000 records it will scan the 10,000 records and retrieve the data and makes performance slow.

Query

Query makes the operation more efficient than scan operation.

Note: To query the table we must pass the partition key. So selecting a proper partition key for the table is important and Query operation will return all items matching with the partition key of the table.

If you use query in DynamoDB you must create an index in the candidate table here, you can create for your own table.

Create Index

Now click the indexes option as shown in above image and click the create index button then a popup will open as shown below.

Creating index popup

In the above popup enter the partition key with any attribute name matching to the particular table , for example in the candidate table email, professional, candidateName and click on submit button.

In the above image I have created two indexes one for email and another for professional for my candidate table.

How to use Query in DynamoDB ?

In the above Scan/Query image choose Query in the first dropdown and choose email-index in the second dropdown and enter the partition key value also add the filter(optional) as shown below in below image.

Query Results

Query syntax for above example in code level:

Query Syntax

In the above image Model is the schema you defined in the project and you can also use filter for query as shown below image.

Query Syntax with filter

You also need to add the indexes in the schema file you defined in the project as shown below.

Candidate Schema
Query Postman Result

In the above image you can see the Scanned Count is 2 when using query using indexes this means it scans only the two items from the list of 8 items. So for large table structure query method is preferred compared to scan method.

Cost benefits of Provisioned Capacity over On-Demand Capacity

The On-Demand capacity is considered more cost compared to provisioned DynamoDB due to its advantages over scaling of databases. However for small applications provisioned capacity is preferred over On-Demand capacity. Please refer here for more details. Also please refer here for read/write capacity and billing costs.

Conclusion:

Here its end In this blog, We saw brief summary of scan and query in DynamoDB with use cases in simple steps.

--

--

Ganeshrp
Ganeshrp

No responses yet