Search code examples
google-cloud-platformgoogle-cloud-spanner

How to force using primary key in Google cloud spanner queries?


There're examples of forcing a named index in Google cloud spanner queries here: https://cloud.google.com/spanner/docs/sql-best-practices#postgresql_2

I would like to know about the syntax of forcing primary key index.


Solution

  • You can reference the primary key in the same way as any other index. Primary keys in Cloud Spanner are always named PRIMARY_KEY. So indicating that the primary key should be used can be achieved like this. The link you shared seems to reference the PostgreSQL-dialect of Google Cloud Spanner, so the example uses that dialect:

    select * from test/*@ FORCE_INDEX=PRIMARY_KEY */;