
We can use the sparse secondary index to help our query.įirst, let’s design the key schema for our secondary index. A single record label will have a huge number of songs, many of which will not be platinum. In our music example, perhaps we want to find all the songs from a given record label that went platinum. The most common way is to narrow down a large collection based on a boolean or enum value. This is where you notion of sparse indexes comes in - you can use secondary indexes as a way to provide a global filter on your table through the presence of certain attributes on your items. DynamoDB will handle all the work to sync data from your main table to your secondary index.ĭynamoDB will only include an item from your main table into your secondary index if the item has both elements of the key schema in your secondary index. The key schema is comparable to the primary key for your main table, and you can use the Query API action on your secondary index just like your main table. When creating a secondary index, you will specify the key schema for the index.

This makes it easy to support additional access patterns. Secondary indexes are a way to have DynamoDB replicate the data in your table into a new structure using a different primary key schema. With DynamoDB, you can create secondary indexes. In this section, we’ll look at a different tool for filtering - the sparse index.
#Javascript client ui for local dynamodb how to
In the last example, we saw how to use the partition key to filter our results.

This filters out all other items in our table and gets us right to what we want. The key condition expression in our query states the partition key we want to use - ALBUM#PAUL MCCARTNEY#FLAMING PIE. You might think you could use the Scan operation with a filter expression to make the following call:Ĭonst AWS = require ( 'aws-sdk' ) const client = new AWS. Imagine you wanted to find all songs that had gone platinum by selling over 1 million copies. In addition to information about the album and song, such as name, artist, and release year, each album and song item also includes a Sales attribute which indicates the number of sales the given item has made.

Albums have a sort key of ALBUM# while songs have a sort key of SONG#. In your table, albums and songs are stored within a collection with a partition key of ALBUM#. Imagine you have a table that stores information about music albums and songs. Let’s walk through an example to see why filter expressions aren’t that helpful. In the next section, we’ll take a look why. However, filter expressions don’t work as you think they would. This sounds tempting, and more similar to the SQL syntax we know and love. The FilterExpression promises to filter out results from your Query or Scan that don’t match the given expression. At this point, they may see the FilterExpression property that’s available in the Query and Scan API actions in DynamoDB. This can feel wrong to people accustomed to the power and expressiveness of SQL. As such, you will use your primary keys and secondary indexes to give you the filtering capabilities your application needs. This is because DynamoDB won’t let you write a query that won’t scale. With DynamoDB, you need to plan your access patterns up front, then model your data to fit your access patterns. Once you’ve properly normalized your data, you can use SQL to answer any question you want.ĭynamoDB is not like that. With this flexible query language, relational data modeling is more concerned about structuring your data correctly. Find all orders in the last week SELECT * FROM orders
#Javascript client ui for local dynamodb free
Feel free to watch the talk if you prefer video over text. This post contains some concepts from my Data Modeling with DynamoDB talk at AWS re:Invent 2019. What to use instead of filter expressions.The allure of filter expressions for DynamoDB novices.In this post, we’ll learn about DynamoDB filter expressions. But filter expressions in DynamoDB don’t work the way that many people expect. Lots of people think they can use a filter expression in their Query or Scan operations to sift through their dataset and find the needles in their application’s haystack. I’m going to shout my advice here so all can hear:įilter Expressions won't save your bad DynamoDB table design. Yet there’s one feature that’s consistently a red herring for new DynamoDB users - filter expressions.

Primary keys, secondary indexes, and DynamoDB streams are all new, powerful concepts for people to learn. For many, it’s a struggle to unlearn the concepts of a relational database and learn the unique structure of a DynamoDB single table design. Over the past few years, I’ve helped people design their DynamoDB tables.
