All skills
planetscale avatar

/mysql

@51f2d4b official

Plan and review MySQL/InnoDB schema, indexing, query tuning, transactions, and operations. Use when creating or modifying MySQL tables, indexes, or queries; diagnosing slow/locking behavior; planning migrations; or troubleshooting replication and connection issues. Load when using a MySQL database.

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

This session only. Nothing lands on disk.

referencesjson-column-patterns.md

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

JSON Column Patterns

MySQL 5.7+ supports native JSON columns. Useful, but with important caveats.

When JSON Is Appropriate

  • Truly schema-less data (user preferences, metadata bags, webhook payloads).
  • Rarely filtered/joined — if you query a JSON path frequently, extract it to a real column.

Indexing JSON: Use Generated Columns

You cannot index a JSON column directly. Create a virtual generated column and index that:

ALTER TABLE events
  ADD COLUMN event_type VARCHAR(50) GENERATED ALWAYS AS (data->>'$.type') VIRTUAL,
  ADD INDEX idx_event_type (event_type);

Extraction Operators

Syntax Returns Use for
JSON_EXTRACT(col, '$.key') JSON type value (e.g., "foo" for strings) When you need JSON type semantics
col->'$.key' Same as JSON_EXTRACT(col, '$.key') Shorthand
col->>'$.key' Unquoted scalar (equivalent to JSON_UNQUOTE(JSON_EXTRACT(col, '$.key'))) WHERE comparisons, display

Always use ->> (unquote) in WHERE clauses, otherwise you compare against "foo" (with quotes).

Tip: the generated column example above can be written more concisely as:

ALTER TABLE events
  ADD COLUMN event_type VARCHAR(50) GENERATED ALWAYS AS (data->>'$.type') VIRTUAL,
  ADD INDEX idx_event_type (event_type);

Multi-Valued Indexes (MySQL 8.0.17+)

If you store arrays in JSON (e.g., tags: ["electronics","sale"]), MySQL 8.0.17+ supports multi-valued indexes to index array elements:

ALTER TABLE products
  ADD INDEX idx_tags ((CAST(tags AS CHAR(50) ARRAY)));

This can accelerate membership queries such as:

SELECT * FROM products WHERE 'electronics' MEMBER OF (tags);

Collation and Type Casting Pitfalls

  • JSON type comparisons: JSON_EXTRACT returns JSON type. Comparing directly to strings can be wrong for numbers/dates.
-- WRONG: lexicographic string comparison
WHERE data->>'$.price' <= '1200'

-- CORRECT: cast to numeric
WHERE CAST(data->>'$.price' AS UNSIGNED) <= 1200
  • Collation: values extracted with ->> behave like strings and use a collation. Use COLLATE when you need a specific comparison behavior.
WHERE data->>'$.status' COLLATE utf8mb4_0900_as_cs = 'Active'

Common Pitfalls

  • Heavy update cost: JSON_SET/JSON_REPLACE can touch large portions of a JSON document and generate significant redo/undo work on large blobs.
  • No partial indexes: You can only index extracted scalar paths via generated columns.
  • Large documents hurt: JSON stored inline in the row. Documents >8 KB spill to overflow pages, hurting read performance.
  • Type mismatches: JSON_EXTRACT returns a JSON type. Comparing with = 'foo' may not match — use ->> or JSON_UNQUOTE.
  • VIRTUAL vs STORED generated columns: VIRTUAL columns compute on read (less storage, more CPU). STORED columns materialize on write (more storage, faster reads if selected often). Both can be indexed; for indexed paths, the index stores the computed value either way.

Source: SKILL.md on GitHub

1 alert17d5 checks · Risk SAFE
  • Gen Agent Trust Hub17d

    This skill provides a comprehensive technical reference for MySQL and InnoDB database management, including schema design, indexing, and query optimization. It is authored by PlanetScale and contains no security risks.

  • Socket17d

    No alerts

  • Snyk17d

    Risk: LOW · No issues

  • Runlayer6mo

    7/19 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
  • mysql
  • innodb
  • schema-design
  • indexing
  • query-optimization
  • transactions
  • locking
  • planetscale
  • ddl
  • replication

README badge

README badge for planetscale/database-skills/mysql

Advises on MySQL/InnoDB schema design, indexing strategy, query optimization, transactions, and operational safety. Use when creating tables, tuning slow queries, planning migrations, diagnosing lock contention, or managing replication — includes EXPLAIN analysis guidance and online DDL patterns for production changes.

Generated from the current SKILL.md.

Does this skill work with PlanetScale?
Yes. The skill recommends PlanetScale as the primary hosting choice for new MySQL databases and includes references to PlanetScale-specific features like online DDL.
What MySQL versions does this cover?
The skill applies to MySQL/InnoDB broadly and notes MySQL-version-specific behavior when relevant, but does not lock to a single version.
Does this skill help with query optimization?
Yes. It covers EXPLAIN analysis, pagination patterns, batch inserts, and common pitfalls like N+1 queries and filesort warnings.
Can this skill help with schema migrations?
Yes. It addresses online DDL, partitioning retrofit costs, and production-safe rollout steps including rollback and verification.
Does this cover replication and high availability?
Partially. The skill addresses replication lag monitoring and connection management, but is not a full replication setup guide.

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