0

I want to store the information of user and their total_score. Each user has a facebook_id and it will be the primary key. I designed the table and index like that:

Table name: UserDataTest
Primary partition key: facebook_id (String)

enter image description here

Now I want to get top 50 user follow by total_ranking. I wrote the query params like that:

var params = {
    TableName : "UserDataTest",
    ProjectionExpression:"facebook_id, facebook_name, #lev, total_star, total_crown, total_ranking, isVip",
    IndexName: "facebook_id-total_ranking-index",
    ExpressionAttributeNames: {
      "#lev" : "level"  
    }
    ScanIndexForward: false,
    Limit: 50
};

But i could not get the data that I need.
Can you give me a solution for this? Any idea for rewrite the query or redesign the table? Thanks

TccHtnn
  • 916
  • 2
  • 8
  • 12
  • You want to search data according to facebook_id, right? – Ashish Kadam May 31 '18 at 05:26
  • No, I want to get 50 user who has the biggest total_ranking. – TccHtnn May 31 '18 at 09:43
  • 1
    Partition key and sort key don't need to be unique for GSI's. Your query is little tricky and I don't know best solution but I believe this should work albeit a little hacky: set partition key to an attribute that is the same for all items, set sort key to total_ranking. Query results are always sorted by the sort key, so you would just need to query the partition key, limit 50 and order descending. this should give you the top 50 total rankings. – Jacob Lange May 31 '18 at 16:52
  • @jake.lange thanks for your answer. I am using this solution, but I think this is not a good solution because I have about 10 million users. So I still think about another design – TccHtnn Jun 01 '18 at 07:22
  • Yeah it's a bad solution cause it will only ever use one partition, with 10 million records it's gonna kill the read capacity instantly. I think this question is pretty similar to what you're asking if you haven't seen it: https://stackoverflow.com/questions/21794945/dynamodb-scan-in-sorted-order – Jacob Lange Jun 01 '18 at 16:37

0 Answers0