Professional Cloud Database Engineer free question 2 / 10 · Domain 1: Design innovative, scalable, and highly available cloud database solutions
You are adding a foreign key in Spanner, and the referenced column already has a secondary index that a user created. Which statement about index management for the foreign key is correct?
- Foreign key validation scans the base table, and no supporting index is created for it
- You specify the name of the user-created index in the foreign key definition
- The foreign key creates its own backing index and does not use the existing user index
- If a user index exists on the same column, it is reused as the index for the foreign key
Show the answer and explanation
Answer: C
Why this is correct
The Foreign key characteristics section of the foreign keys overview says that a foreign key does not use a secondary index created by the user, and instead creates its own backing index. The backing index can be used for query evaluation and with force_index, and its name can be looked up in INFORMATION_SCHEMA. Even if an index on the same column already exists, a separate index is created for the foreign key, so you plan for that extra storage and write cost.
Why the other options are wrong
"If a user index exists on the same column, it is reused as the index for the foreign key" … A foreign key does not use an index that a user created. Even when an index on the same column exists, a separate one is built for the foreign key.
"Foreign key validation scans the base table, and no supporting index is created for it" … It is wrong that no index is created for the foreign key. You should evaluate the additional storage and write overhead.
"You specify the name of the user-created index in the foreign key definition" … The design does not let a foreign key definition name a user index of your choice. Spanner manages the index that it needs.
Key point
You can find managed indexes by checking SPANNER_IS_MANAGED in INFORMATION_SCHEMA. A managed index cannot be dropped or changed the way an ordinary index can.