title: How can I query dynamodb table sorted by `timestamp`?
body: <p>I have a dynamodb table has a partition key as <code>ID</code> and sort key <code>timestamp</code>. I need to query the table to find the latest 100 rows based on <code>timestamp</code> column. When I check the <code>query</code> API, I must specify the <code>KeyConditions or KeyConditionExpression</code>. I don't want to query partition key in this case. How can I do that? </p>
  
  <p>The <code>scan</code> operation doesn't seem support sort the result.</p>
  
  <p>One solution I can think of is to add an attribute on the table, let say <code>status</code> and assign the same value for every rows in this table. Then create a GSI on the table with the partition key as <code>status</code> and <code>timestamp</code> as sort key. Then I can query the table with partition key is equal to <code>OK</code> and sort the result based on <code>timestamp</code>. However, this solution doesn't work well in terms of scale. The GSI will not be scaled well since every row in the table has the same value for <code>status</code>. </p>
  
  <p>So what is the best solution to my problem? Shouldn't I consider using dynamodb?</p>