Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autoincrement in Google Cloud Spanner

Google Cloud Spanner recommends against using monotonically increasing integers for primary keys. This makes a perfect sense for top-level tables as it would create hotspots. But what about interleaved tables? They are going to be stored on the same node anyway. They are also kept in a sorted order so using UUID or some other random ID seems suboptimal, especially if they are going to be retrieved in the sorted order as well.

Is reading the max id and storing maxId + 1 for a new row the right solution here?

like image 589
Jakub Vrána Avatar asked Oct 17 '22 17:10

Jakub Vrána


1 Answers

The interleaved child tables rows are prefixed with the parent table's key, so it is okay to think of them as non-sequential keys as long the parent key is non-sequential. In this situation it should generally be okay since as you note, this entire key range will be served by one Span Server. If you are concerned about hot spotting a single Span server in this situation it would be best to not interleave the table.

It should also be noted that it's really monotonically increasing or decreasing keys that are recommended against, as they have the same effect, just at opposite sides of the range.

like image 165
Dan McGrath Avatar answered Oct 27 '22 11:10

Dan McGrath