Search code examples
amazon-dynamodbboto3

Which method is more efficient to get a subset of dynamodb items?


I have a dynamodb table and a list of all primary keys of the table.

a sample item might a PK of "foo_12424" and SK of "bar_32456"

I want to get all items with sk that starts with the prefix "bar_3"

I can think of doing this in two ways:

  1. Just scan the dynamodb table with the condition that sk has to start with bar_3
  2. loop over the list of all pk and query for SK that begins with bar_3 and return the subset into a list.

From my understanding scan is very inefficient my thought was that querying the sort key would be more efficient since it doesn't go through all items but I'm wondering if the overhead of looping over all primary key negates this advantage?


Solution

  • Essentially what you need here is global sort order based on SK. For that, its often useful to have an index with a common attribute as PK, and then your requests become highly efficient and fast.

    You can understand more from the blog post here.

    GSI_PK SK PK
    1 bar_32456 foo_12424
    1 bar_33453 foo_12111
    1 bar_34344 foo_12222