Simplify Your DynamoDB Queries Using Secondary Index Scans
Let’s say you are building a data-intensive application. It might be for gaming, mobile, Internet of Things (IoT) or simply a modern web application. You are customer-driven and you derive satisfaction from being able to rapidly respond to the ever-changing needs of your users. Their requirements are quickly evolving and the application needs to evolve just as fast. One day they’ll ask you to start storing additional information; the next they’ll ask you for better retrieval functions keyed off of that new information.
Amazon DynamoDB is a great fit for an application built in the environment that I just described. Unlike traditional relational databases, DynamoDB provides a fast and flexible database that has no schema and supports JSON objects. This means that you can begin to store additional information (new attributes and values) as the data becomes available without having to alter the table definition or modify the existing items. As you receive requests to retrieve this information in new ways, you can use Global Secondary Indexes for flexibility and efficiency (you can even create them online with no down time).
As you may know, you have three query options for your DynamoDB tables:
- You can use a
GetItemoperation to retrieve a specific item,
- You can use a
Queryoperation to retrieve specific items based on conditions, or
- You can use a
Scanoperation to retrieve all items.
Secondary Index Scans
Today we are giving you the ability to perform Scan operations on your table’s local and global secondary indexes. This will allow you to efficiently retrieve the precise set of attributes that you have projected in to the index. You can also apply a filter to the results in order to retrieve only the items which meet one or more conditions. Because you can choose to project any desired subset of the table’s attributes in to the index, you can create slim indexes that will make efficient use of your provisioned read throughput. The effect of this feature will become more and more pronounced as you add additional attributes to some or all of the items in the table.
You can use this new feature from the DynamoDB API by including the name of the desired index in your call to the Scan function. You can scan local or global secondary indexes. Scans on local secondary indexes can ask for non-projected attributes; scan on global secondary indexes cannot. The indexed attribute determined the order of the results returned by a scan operation.
The AWS Management Console now supports scanning on secondary indexes. Suppose I have the following items in my table:
And the following Global Secondary Indexes:
I can scan on an index by clicking on Explore Table, choosing the desired index from the dropdown menu, and clicking on Start New Scan:
As you can see, the results contain only the attributes that I projected in to the index. I can further qualify the scan by adding a filter:
This feature is available now and you can start using it today! The cost of a secondary index scan (measured in read capacity units) is identical to the cost of a query on the index. Local index scans that do not filter on or request non-projected attributes cost the same as a regular scan on the same table.
PS – In my sample data, birth dates for Luke and his father are based on the Tho Yor Arrival (in case you were wondering).