Is index creation required for composite key in spanner?

177 Views Asked by At

I have a table in spanner the DDL is given below

CREATE TABLE orders_pii(
    order_id STRING(64) NOT NULL,
    retailer_id STRING(64) NOT NULL,
    site_id STRING(64) NOT NULL,
    shipping_labels ARRAY<STRING(MAX)>,
    order_creation_timestamp TIMESTAMP NOT NULL,
    order_stage_by_timestamp TIMESTAMP NOT NULL,
) PRIMARY KEY(order_id, retailer_id, site_id)

I read the documentation of spanner it says

Spanner automatically creates an index for each table's primary key. For example, you don't need to do anything to index the primary key

But in my case the primary key is combination of 3 columns So will the index be created for all three columns separately or directly the primary key which is combination of 3 columns.

Do I need to create the separate index for these 3 columns ?

2

There are 2 best solutions below

0
On

One unique index will automatically be created for the three columns together. You do not need to manually add any additional indexes for that.

0
On

According to the official documentation it is mentioned that a composite primary key which has one or more of the columns is defined as the table's primary key, which uniquely identifies each row. As also mentioned, Spanner automatically creates an index for each table's primary key ,also it will create the index on the composite primary key in order of the defined columns and the Cloud Spanner tries to pick the best suitable indexes (or use the default one) if it is confident that the index will be better for performance and query results. You can also create secondary indexes for other columns. Adding a secondary index on a column makes it more efficient to look up data in that column. I would also recommend you to check the best practices to execute queries and this link