DEV Community

loading...

Querying AWS DynamoDB on Non-key Columns

Emiya
I am the bone of my sword
・1 min read

Your client loves AWS DynamoDB

Yes, the Amazon's NoSQL database service is a key-value and document database, which anyone who has Googled it should already know. Yet, your client demands querying DynamoDB on non-key columns without the actual keys, as they may have had some constraints on which services to use for their projects, or perhaps just didn't think through.

Fortunately, AWS DynamoDB provides a very quick way to realise such feature: The Global Secondary Index (GSI), which essentially indexes over desired column(s) so that you can later query on them as though they are keys. To do this, simply navigate to your table via the DynamoDB console and click on the Indexes tab, then create indexes with your desired keys.

Below is a Python example for querying the generated index using Boto3, the AWS SDK for Python.

import boto3
from boto3.dynamodb.conditions import Key

dynamodb = boto3.resource('dynamodb', endpoint_url='https://your-dynamodb-endpoint')
table = dynamodb.Table('your_table')

results = table.query(
    IndexName='your_index_name',
    KeyConditionExpression=Key('your_index_key').eq('your_index_value')
)

Alternatively, use Table.scan() with FilterExpression, which obtains the entire table then filters the result, may often be more expensive than indexing.

Discussion (0)