Skip to main content
In Database quickstart you created a single-shard Neki database with categories and products. Those tables stay on the original shard while you add a second shard and create an orders table sharded by a tenant_id column.

Prerequisites

  1. You will need a PlanetScale account
  2. An administrator of your PlanetScale organization must join the Neki Platform Preview.
This quickstart configures the placement of an empty table. For a table that already contains rows, plan a data migration.

Add a shard

Use the same database and main branch from the database quickstart guide. Each new shard will use the selected configuration profile’s cluster size and replica count.
1
Open the dashboard, then go to Clusters and select the default configuration profile.
2
Open the Shards tab and select Create new shards.
3
Enter 1 for the number of shards to add, review the estimated cost, and select Add shards.
4
Wait until the new shard is ready.
Clusters Shards tab showing two ready shards, sh1 and sh2Clusters Shards tab showing two ready shards, sh1 and sh2

Inspect the default topology

Go to Clusters > Data topology to view and edit the JSON document in the dashboard. Alternatively, make the following changes with the PlanetScale CLI. A new Neki database starts with one unsharded shard group. That group is both the default and the authoritative shard group.
The document looks like this, with your first shard’s UID in place of <SHARD_1>:
categories and products inherited the default shard group, so they stay on <SHARD_1>. The authoritative shard group must resolve to exactly one complete shard range. Neki uses that group for cluster-wide metadata and sequences, including identity columns. Changing it is a topology-wide operational change; verify that the new group is complete and available before applying the replacement document. Save a copy of the current document before you replace it.

Create the orders table

Create a schema for the orders table. Do not insert rows yet.

Apply a two-shard topology

Replace <SHARD_1> and <SHARD_2> with the shard name from pscale branch shard list. The orders_by_tenant group splits the keyspace at 80. orders uses xxhash on tenant_id. Everything else keeps the original default.
Save that document as data-topology.json and apply it via the CLI, or paste the same document into Clusters > Data topology and select Save changes.
Confirm the resolved bindings:
orders should list under orders_by_tenant. categories and products should still resolve to <SHARD_1>. See Data topology for shard-index types, defaults, the dashboard editor, and the SQL __neki.set_data_topology metafunction.

Insert and check routing

Insert two tenants. Neki hashes tenant_id and sends each row to the matching shard in orders_by_tenant.
A predicate on tenant_id can narrow the query to one shard:
Route [EqualUnique] means the query can be sent to a single shard.
Web console EXPLAIN (NEKI_PLAN) showing Route EqualUnique on orders_by_tenantWeb console EXPLAIN (NEKI_PLAN) showing Route EqualUnique on orders_by_tenant
A query without that predicate can scatter across the group:
Route [Scatter] means each shard computes a partial count, and Neki combines the results. <SHARD_1> still holds categories and products. Rows added to the orders table are now split across two shards.

Need help?

Get help from the PlanetScale Support team, or join our Discord community to see how others are using PlanetScale.