DynamoDB Data Types and Indexes
In DynamoDB, all data is stored in documents. Each document is represented as a JSON object, and documents can contain any number of attributes. DynamoDB supports several data types for attributes, including:
Data Types
- String: Unicode character strings.
- Number: Number values, both integers and floating-point numbers.
- Binary: Binary data, such as images or compressed files.
- Boolean: Boolean values, true or false.
- Null: Null values.
- String Set: A set of string values.
- Number Set: A set of number values.
- Binary Set: A set of binary values.
Syntax
{
"AttributeName": {"DataType": AttributeValue}
}
Example
{
"Item": {
"ID": {"N": "123"},
"Name": {"S": "John Doe"},
"Tags": {"SS": ["tag1", "tag2"]},
"Pictures": {"BS": ["binary1", "binary2"]},
"IsApproved": {"BOOL": true},
"DateCreated": {"N": "1637473600"}
}
}
Output
{
"Item": {
"ID": {"N": "123"},
"Name": {"S": "John Doe"},
"Tags": {"SS": ["tag1", "tag2"]},
"Pictures": {"BS": ["binary1", "binary2"]},
"IsApproved": {"BOOL": true},
"DateCreated": {"N": "1637473600"}
}
}
Explanation
This document contains several attributes, each with a different data type. The ID attribute is a number value, the Name attribute is a string value, the Tags attribute is a string set, the Pictures attribute is a binary set, the IsApproved attribute is a boolean value, and the DateCreated attribute is a number value representing a Unix timestamp.
Use
Data types are important to consider when designing a DynamoDB schema, as they can affect query performance and storage requirements. Choosing the right data type for each attribute can help optimize query performance and reduce storage costs.
Important Points
- DynamoDB indexes can improve query performance by allowing for efficient lookups of data based on certain attributes.
- There are two types of indexes in DynamoDB: global secondary indexes and local secondary indexes.
- Global secondary indexes allow for efficient lookups of data across all partitions in a table, while local secondary indexes allow for efficient lookups of data within a single partition.
- When creating indexes, it's important to choose the right partition key and sort key to ensure efficient query performance.
Summary
DynamoDB supports several data types for attributes, including string, number, binary, boolean, null, string set, number set, and binary set. Choosing the right data type for each attribute can help optimize query performance and reduce storage costs. DynamoDB indexes can improve query performance and there are two types of indexes in DynamoDB: global secondary indexes and local secondary indexes. Choosing the right partition key and sort key when creating indexes is important to ensure efficient query performance.