0
0
DynamoDBquery~5 mins

Index capacity and cost in DynamoDB

Choose your learning style9 modes available
Introduction

Indexes help you find data faster in a database. But they use extra space and cost more to run.

When you want to quickly find items by attributes other than the main key.
When you need to run queries that filter or sort data in different ways.
When you want to improve read performance for specific queries.
When you want to avoid scanning the whole table for certain searches.
Syntax
DynamoDB
No direct code syntax for capacity; capacity is managed by settings and usage.

Indexes consume read and write capacity units separately from the main table.

More indexes mean more cost and storage use.

Examples
This example shows how you set read and write capacity for an index when creating it.
DynamoDB
Create a Global Secondary Index (GSI) with provisioned capacity:

{
  "IndexName": "MyIndex",
  "KeySchema": [
    {"AttributeName": "Category", "KeyType": "HASH"},
    {"AttributeName": "CreatedDate", "KeyType": "RANGE"}
  ],
  "Projection": {"ProjectionType": "ALL"},
  "ProvisionedThroughput": {
    "ReadCapacityUnits": 5,
    "WriteCapacityUnits": 2
  }
}
Each index you add increases your total capacity cost.
DynamoDB
Read capacity units (RCUs) and write capacity units (WCUs) are charged separately for each index.
Sample Program

This command creates a table with a global secondary index. The index has its own read and write capacity units, which add to the total cost.

DynamoDB
aws dynamodb create-table \
  --table-name Music \
  --attribute-definitions \
    AttributeName=Artist,AttributeType=S \
    AttributeName=SongTitle,AttributeType=S \
    AttributeName=Genre,AttributeType=S \
  --key-schema \
    AttributeName=Artist,KeyType=HASH \
    AttributeName=SongTitle,KeyType=RANGE \
  --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \
  --global-secondary-indexes \
    '[{
      "IndexName": "GenreIndex",
      "KeySchema": [
        {"AttributeName":"Genre","KeyType":"HASH"}
      ],
      "Projection": {"ProjectionType":"ALL"},
      "ProvisionedThroughput": {
        "ReadCapacityUnits": 2,
        "WriteCapacityUnits": 1
      }
    }]'
OutputSuccess
Important Notes

Indexes increase your storage and throughput costs because they keep copies of data.

Write operations cost more because data must be written to the table and all indexes.

Monitor your index usage to avoid paying for unused capacity.

Summary

Indexes speed up queries but use extra capacity and cost more.

Each index has its own read and write capacity units.

Plan indexes carefully to balance performance and cost.