0

With DynamoDB, there is simply no straightforward way to perform an indexed range query over a column. Primary key, local secondary index, and global secondary index all require a partition key to range query.

For example, suppose I have a high-scores table with a numerical score attribute. There is no way to get the top 10 scores or top scores 25 to 50 with an indexed range query

So, what is the idiomatic or preferred way to perform this incredibly common task?

  1. Settle for a table scan.

  2. Use a static partition key and take advantage of partition queries.

  3. Use a fixed number of static partition keys and use multiple partition queries.

1
  • 2
    In addition to the answer left by @Maurice, you may want to search for examples of DynamoDB Leaderboards. There are several articles on the topic with several examples. As you've said, this is a fairly common use case and there are some good examples floating around. Commented Nov 15, 2021 at 11:25

2 Answers 2

3

It's either 2) or 3) but it depends on the amount and structure of data as well as the read/write activity.

There's no generic answer here as it's use-case specific.

As long as you can get away with it, you probably want to use 2) as it only requires a single Query API call. If you have lots of data or heavy read/write activity, you'd use some bucketing-strategy (very close to your third option) to write to multiple partitions, then do multiple queries and aggregate the results.

Sign up to request clarification or add additional context in comments.

Comments

1

DDB isn't suited for analytics. As Maurice said you can facilitate what you need via secondary index, but there are also other options to consider:

  1. If you are providing this Top N to your customers consistently/frequently and N is fixed, then you can have dedicated item(s) that hold this information and you would update that/those item(s) upon writing an item to a table. You can have 1 item for the whole top N or you can apply some bucketing strat.

  2. If your system needs this information infrequently (on some singular occasions), then scan might be also fine.

  3. If this is for analytics/research, consider exporting the table to S3 and using Athena.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.