DynamoDB & Global Secondary Index

Lab Details

  1. This lab walks you through the steps to create a DynamoDB database and use Global Secondary Indexes in a case study.

  2. Duration: 45 minutes

  3. AWS Region: US East (N. Virginia) us-east-1

Introduction

Definition 

  • DynamoDB is a fast and flexible NoSQL database and for applications that need consistent single digit millisecond latency at any scale.  It is a fully managed database and it supports both document and key value data models.

  • It has a very flexible data model. This means that you don't need to define your database schema upfront. Performance is great as well.

  • And all of these attributes make it a great fit for mobile gaming, ad-tech, IoT and many other applications.

DynamoDB Tables

DynamoDB tables consist of 

  • Items (Think of a row of data in a table).

  • Attributes (Think of a column of data in a table).

  • Supports key-value and document data structures.

  • Key= the name of the data.  Value= the data itself.

  • Document can be written in JSON, HTML or XML.

DynamoDB- Primary Keys

  • DynamoDB stores and retrieve data based on a Primary key

  • There are 2 types of Primary Key. Keys: A simple primary key (partition key) and a composite key (partition key and sort key).

  • Partition Keys are important because they are the input for an internal hash function which determines the partition or physical location of which the stored data.

  • If you are using the partition key as your Primary key, then no two items will have the same Partition key.

  • 2 items can have the same partition key, but they must have a different sort key.

  • All items with the same partition key are stored together, then sorted according to the sort key value.

What is an Index in DynamoDB

  • In SQL databases, an index is a data structure which allows you to perform queries on specific columns in a table.

  • You select the column that is required to include in the index and run the searches on the index instead of on the entire dataset.

 

In DynamoDB, two types of indexes are supported to help speed-up your queries.

  • Local secondary Index.

  • Global Secondary Index.

Local Secondary Index

  • Can only be created when you are creating the table.

  • Cannot be removed, added or modified later.

  • It has the same partition key as the original table.

  • Has a different Sort key.

  • Gives you a different view of your data, organized according to an alternate sort key.

  • Any queries based on Sort key are much faster using the index than the main table.

Global Secondary Index

  • You can create a GSI on creation or you can add it later.

  • Different partition key as well as different sort key.

  • It gives a completely different view of the data.

  • Speeds up the queries relating to this alternative Partition or Sort Key.

Case Study: Creating a Global Secondary Index

Let's get our hands dirty by creating a table and adding a GSI on the table.

In this example, imagine we want to keep track of orders that were returned by our users. We'll store the date of the return in a ReturnDate attribute. We'll also add a global secondary index with a composite key schema using ReturnDate as the HASH key and UserAmount as the RANGE key.

Architecture Diagram



Join Whizlabs_Hands-On to Read the Rest of this Lab..and More!

Step 1 : Login to My-Account
Step 2 : Click on "Access Now" to view the course you have purchased
Step 3 : You will be taken to our Learn Management Solution (LMS) to access your Labs,Quiz and Video courses