0
0
DynamoDBquery~30 mins

Scan vs Query performance comparison in DynamoDB - Hands-On Comparison

Choose your learning style9 modes available
Scan vs Query Performance Comparison in DynamoDB
📖 Scenario: You are managing a DynamoDB table that stores customer orders. You want to understand the difference in performance between using Scan and Query operations to retrieve data.
🎯 Goal: Build a simple DynamoDB setup with a table of orders, then write code to perform a Scan and a Query operation. Compare how each operation works and understand their performance differences.
📋 What You'll Learn
Create a DynamoDB table named Orders with OrderId as the partition key
Insert 5 sample orders with specific OrderId and CustomerName
Define a variable to hold the CustomerName to query
Write a Query operation to get orders for the given CustomerName
Write a Scan operation to get all orders and filter by CustomerName
💡 Why This Matters
🌍 Real World
Understanding the difference between Scan and Query helps optimize database performance and cost in real DynamoDB applications.
💼 Career
Database developers and cloud engineers often need to choose the right DynamoDB operation to efficiently retrieve data.
Progress0 / 4 steps
1
Create the Orders table with sample data
Create a dictionary called orders with these exact entries representing orders: 'OrderId': 101, 'CustomerName': 'Alice', 'OrderId': 102, 'CustomerName': 'Bob', 'OrderId': 103, 'CustomerName': 'Alice', 'OrderId': 104, 'CustomerName': 'Charlie', 'OrderId': 105, 'CustomerName': 'Bob'. Store these as a list of dictionaries in orders.
DynamoDB
Need a hint?

Use a list of dictionaries. Each dictionary has keys 'OrderId' and 'CustomerName'.

2
Set the customer name to query
Create a variable called customer_to_find and set it to the string 'Bob'.
DynamoDB
Need a hint?

Just assign the string 'Bob' to the variable customer_to_find.

3
Write the Query operation to find orders for Bob
Create a list called query_results that contains only the orders from orders where CustomerName equals customer_to_find. Use a list comprehension with order as the loop variable.
DynamoDB
Need a hint?

Use a list comprehension with order in orders and filter by order['CustomerName'] == customer_to_find.

4
Write the Scan operation to find orders for Bob
Create a list called scan_results that contains all orders from orders but filtered by CustomerName equal to customer_to_find. Use a for loop with order as the loop variable and an if statement inside the loop to append matching orders to scan_results.
DynamoDB
Need a hint?

Initialize scan_results as an empty list. Use a for loop over orders. Inside the loop, use an if statement to check order['CustomerName'] == customer_to_find. Append matching orders to scan_results.