3

I have a DynamoDB table where I need to query on two different attributes, sometimes by one, sometimes by the other one, but never by both at the same time.

Let's say I have an attribute A and attribute B, other attributes are irrelevant here.

I'm thinking of design this table with the Hash Key being the attribute A and a GSI being attribute B. This way I always perform query instead of scan.

Now a question comes to my mind, which query is faster, on Attribute A (which is the Id) or on the Attribute B (GSI)?

If there is some difference I could switch, letting B as Id and A as GSI.

Thanks

danilobraga
  • 165
  • 1
  • 9
  • Hi, interesting question, it is very unlikely to have different performance - generally in any database. "Primary key" and "index" is in all databases I know the same structure with same performance. Keep in mind that the `Hash Key` has to be unique while GSI not – libik Oct 04 '19 at 13:31
  • Hi @RaymondNijland I've already read, many times by the way, but this kind of performance comparison is not there. – danilobraga Oct 04 '19 at 14:01

1 Answers1

1

The GSI is an actual DynamoDB table with all the main table data copied.

It can be more efficient if you project only the attributes you need.

It may be less efficient considering hash keys are not unique and you may end up iterating on those results to find the desired one.

Horatiu Jeflea
  • 7,256
  • 6
  • 38
  • 67