0

I am trying to implement pagination + sorting of records in my rest API using dynamodb.

There are 4 attributes in the table:-

  • a - pk ( partition key)
  • b - SK (range key)
  • createdAt - normal attribute
  • updatedAt - normal attribute

Since dynamodb always sorts data by default on the range key. This will be a problem when I would like to sort based on created or updated with pagination as some data will be on page 1 for 2020 and 2021 years and some data on page 2 will be of 2022 and 2020 if the range key item starts with z ( just an example)

is there any workaround for this where we can sort dynamic data with pagination and other attributes that are not range key? Also i dont want to use index for the attributes as it can ensure cost

8
  • 1
    You need LSI if you want to use different sort key. Commented Oct 29, 2021 at 7:03
  • @Marcin this will be inefficient i think because if tomorrow there 5 attributes that are not range key then i would be needed to create 5 LSI to sort by 5 attributes. Commented Oct 29, 2021 at 7:05
  • 1
    But how else you would want to sort your records? You can only do this on client side if you are not going to use sort key. Commented Oct 29, 2021 at 7:14
  • By createdAt or By Updated At, lets say in future i add views attribute then maybe by views. The only problem is that dynaomdb will always return sorted records by default on range key. there will be somedata which might be missed after pagination. Alternative could be to query complete data sort it and then scan but it can be ineffictibe for large number of records. Commented Oct 29, 2021 at 7:45
  • 1
    Now that is a fun math question, but I'm not trying to reup my certification today. :) Seriously that is the crux of the question. If price is the concern, you'll have to determine how many attributes have to be projected (if required) and how many reads are likely in the sort of first few pages vs. likelihood of full scan being used. Commented Oct 29, 2021 at 8:24

1 Answer 1

2

If you want to search more indices, you must create more indices. It sounds like you need local secondary indices on updated and created. Then when you sort by that, you switch your pagination to using the correct index.

And I understand that you don't want to pay, but that cost is precisely for what you seek, as far as I can tell.

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

1 Comment

I got your point.

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.