11

Is it possible to have multiple fields as range key?

Say I have a table where each row is uniquely identified by <A,B,C>

 -------------------------------
 A   |   B   |  C  |  D   |  E  |
 -------------------------------

where A is the primary hash key

and I want B and C to be the primary range keys.

How can I have more than 2 fields as a primary key in DynamoDB?

Thiyagu
  • 213
  • 1
  • 2
  • 6

2 Answers2

10

Put a copy of B and C in the range field (as an extra column) and make that your range key like "B_C" and you can still also have separate columns for B and C if you need.

Jeff M
  • 254
  • 2
  • 5
9

You can't have more than 2 fields as primary key in DynamoDB.

As a workaround you can create local secondary index for each field that you want to be a range key. But you will not be able to build query with multiple range keys simultaneously because DynamoDB can only use one index at a time.

Consider using filter expressions, you will probably end up using more read throughput (filtering is performed after data is received from db) than you need but get the desired results.