The steps in this documentation are similar to those in the Sharding quickstart, however, there is one important addition that you cannot skip. Be sure to review Step 7: Remove
"require_explicit_routing": true, as it is a crucial step that differs from sharding an unsharded keyspace.- If you are sharding an existing table in an unsharded keyspace, follow the instructions in the Sharding quickstart documentation.
- If you are creating a new table that you want in your existing sharded keyspace, follow the instructions in the Sharding new tables documentation.
- If you simply need to adjust the size of each shard, and not the number of shards, you can do so from the Clusters page in the dashboard.
- Source keyspace: The original sharded keyspace from which you are moving the tables you wish to shard.
- Target keyspace: The new sharded keyspace that you are moving the selected tables to.
pscale branch vtctl move-tables, so make sure you have the pscale CLI installed and authenticated. You must be an Organization Administrator or a Database Administrator of the database.
This guide also assumes that you either are already using @primary in your application code to target your keyspaces or you do not directly set a database name in your application code.
Pre-sharding checklist
There is a small amount of upfront work that needs to happen prior to sharding your table(s) again.1. Prepare to move all table(s) from source keyspace
Some common signals that a table may benefit from further sharding include:- The table has become very large and query performance has degraded due to this
- Schema changes to the table take hours
- You expect the table to grow quickly and want to shard it further before it becomes a problem
2. Create another sharded keyspace
To set up another sharded keyspace:1
Go to the “Clusters” tab in the left nav in the PlanetScale dashboard.
2
Click “New keyspace”.
3
Enter the keyspace name (for example,
metal-sharded-2).4
Select the shard count and choose the cluster size for this keyspace. Keep in mind, creating a sharded
keyspace will use the selected size for each shard. For example, if you are creating 4 shards and choose the
PS-80 cluster size, we will create 4 PS-80s, each with 1 primary and 2 replicas.5
Select the number of additional replicas, if any, that you’d like to add to each cluster. Each cluster comes with
2 replicas by default, so any number you choose will be in addition to those 2.
6
Review the new monthly cost for this keyspace below. This is in addition to your existing unsharded keyspace, as
well as any other keyspaces you add.
7
Once satisfied, click “Create keyspace”.
3. Add "require_explicit_routing": true
If you are using Vitess global routing (for example, if you are using @primary), you will get ambiguous table errors once you add Vindexes to your new keyspace.
To prevent this error, you must temporarily add require_explicit_routing to your new keyspace’s VSchema:
- Safe migrations off: modify the target keyspace VSchema directly on the Clusters page
- Safe migrations on: modify the target keyspace VSchema using deploy requests
4. Copy Vindexes and auto-increment VSchema settings
Assuming your sharding scheme will remain the same, once you’ve completed the previous step of adding"require_explicit_routing": true, you can copy the relevant parts of your source keyspace VSchema into your target keyspace VSchema.
Since we recommend moving all tables from the source to the target keyspace, your target VSchema will look exactly the same as your source VSchema, with the addition of "require_explicit_routing": true. For example, if you are moving the tables users and exercise_logs, and your source keyspace VSchema looks like this:
Move the tables with MoveTables
The examples below use a database namedmydb, the main production branch, the current metal-sharded keyspace as the source, and the new metal-sharded-2 keyspace as the target.
Step 1: Create the workflow
reshard_metal that moves every table from the source keyspace, and starts it right away. To move only some tables, pass --tables with a comma-separated list instead of --all-tables. --defer-secondary-keys creates secondary indexes after the copy finishes, which makes the copy much faster.
Step 2: Watch the copy
As soon as the workflow starts, Vitess copies rows of the tables from your source keyspace to your target keyspace. It uses a combination ofSELECT * FROM table and binlog-based replication, and redistributes the rows across the target keyspace’s shards using the Vindexes in its VSchema.
Check progress with status:
Running: Vitess keeps replicating every new write on the source keyspace to the target keyspace, while the source keyspace still serves all primary and replica traffic.
You can also follow the workflow from the dashboard. Click “Workflows” in the left nav, select your branch, and open the workflow to see its streams, per-table copy progress, replication lag, and traffic routing.
Step 3: Verify data consistency
Once the streams areRunning, verify that the source and target keyspaces hold the same data with a VDiff:
uuid from the output to vdiff show, and repeat until the VDiff completes:
Step 4: Switch replica traffic
Switch replica traffic first to test reads from the new keyspace:--dry-run to see what a switch would do without applying it.
Step 5: Switch primary traffic
When reads from replicas look good, switch primary traffic:Step 6: Check traffic in your application
You should now go check out your production application that uses this database to make sure everything is running as expected. You can also check the Insights tab for errors or slow queries. If you notice an issue, switch traffic back to the source keyspace:Step 7: Remove "require_explicit_routing": true
You should have added require_explicit_routing to your target keyspace’s VSchema in step 3 of the “Pre-sharding checklist”:
Step 8: Complete the workflow
Up until now, you can cancel the workflow or reverse traffic. Completing the workflow is not reversible: it stops replication between the keyspaces and, with the flags below, drops the moved tables from the source keyspace and removes the routing rules. Preview whatcomplete will do with --dry-run first:
"require_explicit_routing": true from your target keyspace VSchema, and you are sure you want to proceed, run the same command without --dry-run.
--keep-data=falsedrops the moved tables from the source keyspace. Add--rename-tablesto rename them instead of dropping them.--keep-data=trueleaves them in place.--keep-routing-rules=falseremoves the routing rules. Pass--keep-routing-rules=trueto keep them if some queries still name the source keyspace.
=: a space-separated value such as --keep-data false is read as --keep-data=true.
Step 9: Check that your production application is working as expected
Finally, check your production application to make sure everything is working as expected. You can check your Insights tab to see if queries are being properly routed to your new keyspace. Insights will also show you any errors, query performance issues, and more. If you moved every table and the source keyspace has no tables left, you can delete it withpscale keyspace delete or from the Clusters page.
That’s it! The tables you selected at the beginning are now being served by the new sharded keyspace.

