AUTO_INCREMENT
for your ID.
In such a setup, the rows of the table are distributed across many instances of MySQL.
The separate instances of MySQL do not have a built-in way to coordinate which IDs are in use and which are not.
Instead, you will need to use a sequence table.
A sequence table is a special table that contains metadata for managing the incrementing ID values for the column of a horizontally sharded table.
Each time you create a new horizontally sharded table, you should create the corresponding sequence table and update the VSchema.
Creating a horizontally-sharded table
We recommend keeping safe migrations enabled for all production databases. Thus, the first step to make schema modifications is to create a new branch, and connect to it via the command line. Next, to create a horizontally-sharded table, switch to your desired sharded keyspace. Create a new table in this keyspace, and do not useAUTO_INCREMENT
for your ID column.
For example, to create a table in the test_sharded
keyspace, run:
_seq
or _sequence
appended.
Being consistent with this naming will help maintain a clear association between your data tables and sequence tables.
SEQUENCE
, let it know to use the id
column as the shard key, and tell it to use the test_seq
table for fetching auto incrementing IDs.
Sequence table values
Unlike vanilla Vitess, PlanetScale will automatically populate the single required row into any sequence table created with the above steps. After merging your deploy request, you should be able to query the sequence table as follows:id
Should always be 0.next_id
represents the next ID in the sequence to be fetched. You typically want this to start as 1.cache
represents the number of IDs that can be fetched and cached by a VTTablet. For good performance, this should be set to a large number like 1000 or more.
1
.
NoteCheck out the Vitess documentation on sequences for more information.