All skills
planetscale avatar

/vitess

@51f2d4b official

Vitess best practices, query optimization, and connection troubleshooting for PlanetScale Vitess databases. Load when working with Vitess databases, sharding, VSchema configuration, keyspace management, or MySQL scaling issues.

Use this Skill: https://skilld.dev/gh/planetscale/database-skills/vitess

This session only. Nothing lands on disk.

referencesquery-serving.md

≈1.5k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Query Serving and MySQL Compatibility

Vitess supports the MySQL protocol and nearly all MySQL syntax. Applications connect to VTGate as if it were a MySQL server, but distributed execution introduces important routing and compatibility differences.

Reference: https://vitess.io/docs/23.0/reference/compatibility/mysql-compatibility/

Query routing

VTGate routes queries based on the VSchema and WHERE clause, targeting the fewest shards possible.

Routing Condition Performance
Single-shard WHERE on primary vindex with = Best
Multi-shard (targeted) WHERE with IN on primary vindex Good
Scatter No primary vindex filter Expensive (all shards)
Unsharded Table in unsharded keyspace Direct to single backend

Always include the primary vindex column in WHERE clauses to avoid scatter queries. If a non-vindex column lookup is unavoidable, a lookup vindex exists as an option (see VSchema skill), but lookup vindexes are expensive. Prefer redesigning the schema or access patterns before reaching for a lookup vindex.

SELECT * FROM orders WHERE customer_id = 42;          -- single-shard (fast)
SELECT * FROM orders WHERE order_date > '2025-01-01'; -- scatter (slow)

Check routing with VEXPLAIN PLAN: look for Route variant EqualUnique (single-shard), IN (targeted multi-shard), or Scatter (all shards). For deeper debugging, VEXPLAIN ALL includes the MySQL query plans from each tablet, and VEXPLAIN TRACE includes metrics on how many rows are passed between parts of the query.

Cross-shard operations

Joins: Cross-shard joins work but are expensive (nested loop joins). Co-locate tables by sharding on the same column so joins stay single-shard.

Aggregations: GROUP BY, ORDER BY, LIMIT, and aggregates work across shards. When grouping on at least one sharding key, Vitess pushes aggregation down to MySQL, then aggregates the per-shard results—making queries fast since shards process different chunks in parallel.

Ordering: As with MySQL itself, queries without ORDER BY have no guaranteed order (MySQL typically returns rows in index order, but this is not contractual). In Vitess this is especially true since results come from multiple shards. Always use ORDER BY when order matters.

Subqueries: Non-correlated subqueries are supported. Correlated subqueries may fail cross-shard; rewrite as JOINs.

Transactions

Mode Behavior
SINGLE Reject transactions spanning multiple shards
MULTI (typical default) Best-effort multi-shard; sequential commits, partial commits possible
TWOPC Two-phase commit for atomic cross-shard writes

Single-shard transactions are fully ACID and support all MySQL isolation levels. Multi-shard transactions also support all isolation levels, but the isolation level is always local to each individual shard. Design schemas to keep transactions within a single shard. Use 2PC when atomic cross-shard writes are required.

MySQL compatibility

Fully supported: Standard DML, DDL (via Online DDL), all JOIN types, non-correlated subqueries, UNION, non-recursive CTEs, prepared statements, LAST_INSERT_ID(), most functions/operators, mysql_native_password/caching_sha2_password, TLS.

Partially supported: Views (experimental, read-only), stored procedures (CALL on unsharded or shard-targeted only), temporary tables (unsharded only), LOAD DATA (unsharded only), UDFs (with --enable-udfs), recursive CTEs (experimental), GET_LOCK/RELEASE_LOCK (with restrictions; routed to a single shard).

Not supported: CREATE PROCEDURE, triggers, events, LOCK TABLES, window functions, CREATE DATABASE/DROP DATABASE. Use application-level logic, external schedulers, or vtctldclient instead.

Auto-increment: MySQL AUTO_INCREMENT is per-shard and produces duplicates. Use Vitess Sequences (see VSchema skill).

Foreign keys: Limited in sharded keyspaces. Prefer application-level referential integrity.

Workload modes

  • OLTP (default): strict timeouts and row-count limits. Configure via --queryserver-config-query-timeout and --queryserver-config-transaction-timeout.
  • OLAP: SET workload = 'olap'; for relaxed limits on analytical queries.

Per-query timeout: SET query_timeout_ms = 5000;

Kill queries: KILL <connection_id>; or KILL QUERY <connection_id>;

Reference tables

Reference: https://vitess.io/docs/23.0/reference/vreplication/reference_tables/

Reference tables are small, rarely-changing lookup tables (e.g. countries, currencies, product categories) that Vitess replicates to every shard via a Materialize VReplication workflow. The source of truth lives in an unsharded keyspace where all DMLs are executed.

Mark tables with "type": "reference" in the VSchema of both keyspaces (the target also needs a "source" field). SELECTs are then served locally per shard — no cross-shard lookup needed.

Performance checklist

  1. Include primary vindex in WHERE clauses for single-shard routing
  2. Co-locate frequently joined tables with shared vindexes
  3. Consider lookup vindexes as a last resort for secondary access patterns (they add write overhead)
  4. Always use ORDER BY when order matters
  5. Avoid SELECT *; use LIMIT on user-facing queries
  6. Prefer cursor-based pagination over OFFSET
  7. Rewrite correlated subqueries as JOINs
  8. Keep transactions within a single shard
  9. Use OLAP mode for analytical queries
  10. Monitor with VEXPLAIN PLAN to verify query routing; use VEXPLAIN ALL for MySQL query plans and VEXPLAIN TRACE for row-flow metrics
  11. Use reference tables for small, rarely-changing lookup tables to avoid cross-shard joins

Source: SKILL.md on GitHub

1 warning17d5 checks · Risk SAFE
  • Gen Agent Trust Hub17d

    The skill provides comprehensive documentation and best practices for Vitess and PlanetScale databases. It consists entirely of informational markdown files without executable scripts, dependencies, or network operations outside of referencing official vendor resources.

  • Socket17d

    No alerts

  • Snyk17d

    Risk: LOW · No issues

  • Runlayer6mo

    5/6 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 51f2d4b. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub last month.

Activeupdated 7 months ago
Other metadata
metadata
{
  "author": "planetscale",
  "version": "1.0.0",
  "organization": "PlanetScale",
  "date": "February 2026"
}
  • Database
  • vitess
  • planetscale
  • mysql
  • sharding
  • vschema
  • connection-pooling
  • schema-migrations

README badge

README badge for planetscale/database-skills/vitess

Guides query routing, sharding strategy, schema migration, and MySQL compatibility for Vitess databases on PlanetScale. Covers VSchema configuration, keyspace management, cross-shard query performance, and Online DDL workflows.

Generated from the current SKILL.md.

Does Vitess support stored procedures and triggers?
No. Stored procedures, triggers, and events are not supported through VTGate. Application logic must handle these operations.
What should I use for generating IDs on sharded tables?
Use Vitess Sequences (a global counter in an unsharded keyspace) or app-generated IDs like UUIDs or snowflakes to avoid collisions across shards.
Are cross-shard joins supported?
Yes, but they are expensive scatter-gather operations. Filter by the vindex column to force single-shard routing and avoid cross-shard joins when possible.
How do I apply schema changes in production on PlanetScale?
Use PlanetScale deploy requests, which implement non-blocking Online DDL migrations across all shards without disrupting workloads.
Does Vitess support foreign keys?
Foreign keys have limited support in Vitess. Prefer application-level referential integrity checks on sharded keyspaces.

Generated from the current SKILL.md. These answers refresh after source changes.