AWS

4 Mins Read

Multi-Key Support for Global Secondary Index in Amazon DynamoDB for Query Flexibility

Voiced by Amazon Polly

Amazon DynamoDB is widely known for its speed, scalability, and ability to handle massive workloads without compromising performance. One of its most powerful features is the Global Secondary Index (GSI), which allows developers to query data efficiently using alternate keys.

Traditionally, GSIs were limited to a single partition key and a single sort key. However, with the introduction of multi-key support, DynamoDB takes flexibility and performance to the next level. Now DynamoDB supports up to four attributes for the partition key (PK) and four attributes for the sort key (SK) in a composite manner to identify items as part of a GSI.

Multi-key support enables you to define multiple attributes as keys within a single GSI, allowing queries to span across different dimensions without creating multiple indexes. This means you can:

  • Reduce the number of GSIs needed for complex queries.
  • Optimise storage and cost by consolidating indexes.
  • Improve query performance for diverse access patterns.

Start Learning In-Demand Tech Skills with Expert-Led Training

  • Industry-Authorized Curriculum
  • Expert-led Training
Enroll Now

Why Does This Matter?

In real-world applications, data access patterns evolve. For example:

  • IoT systems might query device reading data by location, year, month or timestamp.
  • E-commerce platforms may need to query orders by customer ID, product category, or status.

Previously, you had to create separate GSIs for each pattern, which increased complexity and cost. Multi-key support simplifies this by allowing one GSI to serve multiple query dimensions.

Key Benefits

  • Cost Efficiency: Fewer GSIs mean lower storage and maintenance costs.
  • Simplified Design: Reduce schema complexity and operational overhead.
  • Improved Query Flexibility: Support multiple access patterns without redesigning your database.

Getting Started

To leverage multi-key support for time-series data with hierarchical time attributes (year → month → day → timestamp) for efficient queries at any time granularity without date parsing or manipulation.

Base Table Design:

Table diagram showing DynamoDB IoTReadings schema with attributes: readingId, deviceId, locationId, year, month, day, timestamp.

Figure 1:  DynamoDB table name – IoTReadings

Indexed Table – GSI with multiple key attributes.

Table diagram showing DynamoDB GSI key structure with deviceId, locationId as partition keys and year, month, day, timestamp as sort keys.

Figure 2: Secondary Index name – DeviceLocationTimeIndex

To create a base table and its GSI, create a JSON file named “table.json” shown below:

To create a table using AWS CLI, run the following command shown below.

Added a set of items to a table. Below are sample items added into the table as shown in Figure 3, using the AWS CLI command shown below.

DynamoDB GSI query results showing IoTReadings items filtered by deviceId, locationId, and time attributes in a tabular view.

Figure 3: Items added to the table

Access Pattern Requirement:

  1. All readings for a device in a specific location
  2. Readings for the device in a specific location for a specific year and specific month

Similarly, you may have various requirements that you can fulfil with GSI for reading data related to a specific device, with granularity down to a specific day and time.

To retrieve the data for the access pattern requirement 1, run the command below using the AWS CLI, and the output is shown in Figure 4 below.

Output of access pattern requirement 1, displaying readings of device – D001, in location – L001.

DynamoDB GSI query results showing IoTReadings filtered by deviceId, locationId, year, month, day, and timestamp in table view.

Figure 4: output of query operation on GSI for access requirement 1

To retrieve the data for the access pattern requirement 2, run the command below using AWS CLI.

Output of access pattern requirement 1, displaying readings of device – D001, in location – L001, for year 2024 and month 01.

DynamoDB GSI query result showing a single IoTReading item filtered by deviceId, locationId, year, month, day, and timestamp.

Figure 5: output of query operation on GSI for access requirement 2

For detailed implementation guidance, check the official instructions on using global secondary indexes in DynamoDB, or to know more about DynamoDB or other NoSQL solutions on AWS, you can also check out courses like “Build Modern Applications with AWS NoSQL Databases”.

Flexible DynamoDB Querying

Multi-key support for GSIs in DynamoDB is a significant enhancement for developers building scalable, flexible applications. It reduces complexity, optimises cost, and unlocks new possibilities for data access patterns, all while maintaining DynamoDB’s hallmark performance. This enhancement is a major leap forward for anyone architecting scalable, query-efficient NoSQL solutions on AWS.

Upskill Your Teams with Enterprise-Ready Tech Training Programs

  • Team-wide Customizable Programs
  • Measurable Business Outcomes
Learn More

About CloudThat

CloudThat is an award-winning company and the first in India to offer cloud training and consulting services worldwide. As a Microsoft Solutions Partner, AWS Advanced Tier Training Partner, and Google Cloud Platform Partner, CloudThat has empowered over 850,000 professionals through 600+ cloud certifications winning global recognition for its training excellence including 20 MCT Trainers in Microsoft’s Global Top 100 and an impressive 12 awards in the last 8 years. CloudThat specializes in Cloud Migration, Data Platforms, DevOps, IoT, and cutting-edge technologies like Gen AI & AI/ML. It has delivered over 500 consulting projects for 250+ organizations in 30+ countries as it continues to empower professionals and enterprises to thrive in the digital-first world.

WRITTEN BY Kamlesh N

Kamlesh Nenwani is a Subject Matter Expert at CloudThat, specializing in AWS Architecting and DevOps. With 13 years of experience in training and consultancy, he has guided over 2500+ professionals and students in mastering diverse technologies. Renowned for his clarity in explaining complex topics and his commitment to continuous learning, Kamlesh delivers practical, interactive sessions grounded in deep technical expertise.

Share

Comments

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!